feat(authentication): add full event support

This commit is contained in:
2023-04-13 23:24:11 +02:00
parent 1451240f1a
commit eafe4dc7c6
102 changed files with 1677 additions and 1664 deletions
@@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
@@ -15,41 +15,39 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'dart:async';
import 'dart:math';
import 'package:example_router/core/constants/form_field.dart';
import 'package:example_router/core/dependency_injection/get_it.dart';
import 'package:example_router/core/routes/router.dart';
import 'package:example_router/core/utils/custom_password.dart';
import 'package:example_router/core/utils/forms.dart';
import 'package:example_router/presentation/features/authentication/authentication_cubit.dart';
import 'package:example_router/presentation/features/sign_in/blocs/custom_sign_in_cubit.dart';
import 'package:example_router/presentation/features/sign_up/blocs/custom_sign_up_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
FutureOrResult<int?> onAccountChanges(
AuthenticationRepository<int> repo,
AuthChangeEvent? authEvent,
) async {
final id = Random().nextInt(1000);
final token =
await repo.getIdentityToken().fold((value) => value, (error) => 'null');
// FutureOrResult<int?> onAccountChanges(
// AuthenticationRepository<int> repo,
// AuthChangeEvent? authEvent,
// ) async {
// final id = Random().nextInt(1000);
// final token =
// await repo.getIdentityToken().fold((value) => value, (error) => 'null');
debugPrint(
'onAccountChanges: ${authEvent?.account}, type: ${authEvent.runtimeType}, token: $token, generatedId: $id');
return Ok<int, AppException>(id);
}
// debugPrint(
// 'onAccountChanges: ${authEvent?.account}, type: ${authEvent.runtimeType}, token: $token, generatedId: $id');
// return Ok<int, AppException>(id);
// }
class App extends StatelessWidget {
final AuthenticationRepository<int> authenticationRepository =
AuthenticationRepositoryImpl(
authenticationCacheDataSource: getIt<AuthenticationCacheDataSource<int>>(),
authenticationRemoteDataSource: getIt<AuthenticationRemoteDataSource>(),
onAuthChange: onAccountChanges,
authenticationRemoteDataSource:
getIt<AuthenticationRemoteDataSource<int>>(),
customPasswordValidator: const CustomPassword.pure(),
extraSignUpInputs: [
FormInput(
@@ -66,8 +64,7 @@ class App extends StatelessWidget {
Widget build(BuildContext context) {
AuthenticationState<int>? previous;
final AuthenticationCubit<int> authenticationCubit =
AuthenticationCubit(authenticationRepository: authenticationRepository);
final AuthenticationCubit<int> authenticationCubit = ExampleAuthenticationCubit(authenticationRepository: authenticationRepository);
final GoRouter router = GoRouter(
initialLocation: '/',
@@ -119,7 +116,7 @@ class App extends StatelessWidget {
),
),
BlocProvider<SignInCubit<int>>(
create: (_) => SignInCubit(
create: (_) => CustomSignInCubit(
authenticationRepository: authenticationRepository,
),
),
@@ -0,0 +1,52 @@
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
class ExampleAuthenticationCubit extends AuthenticationCubit<int> {
ExampleAuthenticationCubit({required super.authenticationRepository});
@override
FutureOrResult<int?> onReauthenticate(
Result<Account, AppException> result) async {
print('onReauthenticate');
return const Ok(1);
}
@override
FutureOrResult<int?> onRefresh(Result<Account, AppException> result) {
print('onRefresh');
return const Ok(1);
}
@override
FutureOrResult<int?> onSignInFromCache(SessionWrapper<int> wrapper) {
print('onSignInFromCache');
return const Ok(1);
}
@override
FutureOrResult<void> onSignOut() {
print('onSignOut');
return const Ok(null);
}
}
@@ -1,74 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:example_router/core/constants/form_field.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
class EditProfileCubit extends FormDataCubitImpl {
final AuthenticationRepository<int> authenticationRepository;
EditProfileCubit(
super._formRepository,
super._formName, {
required this.authenticationRepository,
}) : super();
@override
Future<void> submit() async {
emit(
state.copyWith(
status: FormStatus.submissionInProgress,
),
);
// final user = (await authenticationRepository.getAccount()).ok;
final form = state.form;
final email = form.valueOf<String?>(AuthFormField.email);
final oldPassword = form.valueOf<String?>(AppFormField.oldPassword);
final newPassword = form.valueOf<String?>(AuthFormField.password);
if (email.isNullOrEmpty ||
oldPassword.isNullOrEmpty ||
newPassword.isNullOrEmpty) {
emit(
state.copyWith(
errorMessage: 'An error occured while retrieving data from the form.',
status: FormStatus.submissionFailure,
),
);
}
try {
// await authenticationRepository.signInWithEmailAndPassword(
// email: user?.email ?? '',
// password: oldPassword ?? '',
// );
// await authenticationRepository.reauthenticateWithCredential();
await authenticationRepository.updateEmail(email: email!);
await authenticationRepository.updatePassword(password: newPassword!);
} on Exception catch (e) {
emit(
state.copyWith(
status: FormStatus.submissionFailure,
errorMessage: e.toString(),
),
);
}
emit(state.copyWith(status: FormStatus.submissionSuccess));
}
}
@@ -1,61 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:example_router/core/constants/form_name.dart';
import 'package:example_router/presentation/features/edit_profile/blocs/edit_profile_cubit.dart';
import 'package:example_router/presentation/features/edit_profile/widgets/edit_profile_form.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
class EditProfilePage extends StatelessWidget {
const EditProfilePage({Key? key}) : super(key: key);
static String pageName = 'EditProfile';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Edit Profile')),
body: Padding(
padding: const EdgeInsets.all(8),
child: SingleChildScrollView(
child: BlocProvider<EditProfileCubit>(
create: (context) => EditProfileCubit(
context.read<FormRepository>(),
AppFormName.editProfile,
authenticationRepository:
context.read<AuthenticationRepository<int>>(),
)..dataChanged<String?>(
AuthFormField.email,
Email.dirty(
context
.read<AuthenticationCubit<int>>()
.state
.accountWrapper
?.account
?.email ??
'',
),
),
child: const EditProfileForm(),
),
),
),
);
}
}
@@ -1,136 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:example_router/core/constants/form_field.dart';
import 'package:example_router/core/utils/custom_password.dart';
import 'package:example_router/presentation/features/edit_profile/blocs/edit_profile_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
class _EmailInput extends StatelessWidget {
@override
Widget build(BuildContext context) {
return InputBuilderTextController<EditProfileCubit, String?, void>(
field: AuthFormField.email,
builder: ((context, cubit, state, field, input, controller, extra) {
return TextField(
onChanged: (email) => cubit.dataChanged<String?>(
AuthFormField.email, Email.dirty(email)),
keyboardType: TextInputType.emailAddress,
controller: controller,
decoration: InputDecoration(
labelText: 'Email',
helperText: '',
errorText: input.validator.invalid ? 'Invalid email' : null,
),
);
}),
);
}
}
class _OldPasswordInput extends StatelessWidget {
@override
Widget build(BuildContext context) {
return InputBuilder<EditProfileCubit>(
field: AppFormField.oldPassword,
builder: ((context, cubit, state, field, input) {
return TextField(
onChanged: (pwd) => cubit.dataChanged<String?>(
AppFormField.oldPassword, CustomPassword.dirty(pwd)),
obscureText: true,
decoration: InputDecoration(
labelText: 'Old Password',
helperText: '',
errorText: input.validator.invalid ? 'Invalid password' : null,
),
);
}),
);
}
}
class _NewPasswordInput extends StatelessWidget {
@override
Widget build(BuildContext context) {
return InputBuilder<EditProfileCubit>(
field: AuthFormField.password,
builder: ((context, cubit, state, field, input) {
return TextField(
onChanged: (pwd) => cubit.dataChanged<String?>(
AuthFormField.password, CustomPassword.dirty(pwd)),
obscureText: true,
decoration: InputDecoration(
labelText: 'New Password',
helperText: '',
errorText: input.validator.invalid ? 'Invalid password' : null,
),
);
}),
);
}
}
class _EditButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SubmitBuilder<EditProfileCubit>(
builder: ((context, cubit, status) {
return status.isSubmissionInProgress
? const CircularProgressIndicator()
: ElevatedButton(
onPressed: status.isValidated ? () => cubit.submit() : null,
child: const Text('Edit profile'),
);
}),
);
}
}
class EditProfileForm extends StatelessWidget {
const EditProfileForm({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocListener<EditProfileCubit, FormDataState>(
listener: (context, state) {
if (state.status == FormStatus.submissionFailure) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(
SnackBar(content: Text(state.errorMessage ?? 'Edit Failure')),
);
}
},
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
_EmailInput(),
const SizedBox(height: 8),
_OldPasswordInput(),
const SizedBox(height: 8),
_NewPasswordInput(),
const SizedBox(height: 16),
_EditButton(),
],
),
),
);
}
}
@@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
@@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:example_router/presentation/features/edit_profile/edit_profile_page.dart';
import 'package:example_router/presentation/features/sub/sub_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -30,7 +29,7 @@ class HomePage extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
title: Text('Home | ${context.account<AuthenticationCubit<int>, int>()?.email}'),
actions: [
IconButton(
onPressed: () =>
@@ -44,8 +43,8 @@ class HomePage extends StatelessWidget {
child: Column(
children: [
AuthenticationBuilder<int>(
authenticated: (context, accountWrapper) => Text(
'Logged as ${accountWrapper.account?.email} | GeneratedId is ${accountWrapper.data}'),
authenticated: (context, sessionWrapper) => Text(
'Logged as ${sessionWrapper.session?.account.email} | GeneratedId is ${sessionWrapper.session?.data}'),
unauthenticated: (context) =>
const Text('Not logged (unauthenticated)'),
unknown: (context) => const Text('Not logged (unknown)'),
@@ -57,10 +56,10 @@ class HomePage extends StatelessWidget {
onPressed: () => context.pushNamed(SubPage.pageName),
child: const Text('Go to sub page'),
),
ElevatedButton(
onPressed: () => context.pushNamed(EditProfilePage.pageName),
child: const Text('Go to edit profile page'),
),
// ElevatedButton(
// onPressed: () => context.pushNamed(EditProfilePage.pageName),
// child: const Text('Go to edit profile page'),
// ),
],
),
),
@@ -0,0 +1,48 @@
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_architecture/src/domain/usecases/usecase.dart';
import 'package:wyatt_architecture/src/core/exceptions/exceptions.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:wyatt_type_utils/src/either/either_base.dart';
import 'package:wyatt_form_bloc/src/domain/form/wyatt_form.dart';
class CustomSignInCubit extends SignInCubit<int> {
CustomSignInCubit({
required super.authenticationRepository,
});
@override
FutureOrResult<int?> onSignInWithEmailAndPassword(Result<Account, AppException> result, WyattForm form) {
print('onSignInWithEmailAndPassword');
return const Ok(1);
}
@override
FutureOrResult<int?> onSignInAnonymously(Result<Account, AppException> result, WyattForm form) {
print('onSignInAnonymously');
return const Ok(1);
}
@override
FutureOrResult<int?> onSignInWithGoogle(Result<Account, AppException> result, WyattForm form) {
print('onSignInWithGoogle');
return const Ok(1);
}
}
@@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
@@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
@@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
@@ -14,33 +14,34 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'dart:async';
import 'package:example_router/core/constants/form_field.dart';
import 'package:flutter/foundation.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
class CustomSignUpCubit extends SignUpCubit<int>{
class CustomSignUpCubit extends SignUpCubit<int> {
CustomSignUpCubit({
required super.authenticationRepository,
});
@override
FutureOrResult<void> onSignUpWithEmailAndPassword(
Result<Account, AppException> result, WyattForm form) async {
if (result.isOk) {
await Future.delayed(const Duration(seconds: 3));
const id = -1;
final confirmedPassword =
form.valueOf<String?>(AppFormField.confirmedPassword);
FutureOrResult<int?> onSignUpWithEmailAndPassword(Result<Account, AppException> result, WyattForm form) async {
// if (result.isOk) {
// await Future.delayed(const Duration(seconds: 3));
// const id = -1;
// final confirmedPassword =
// form.valueOf<String?>(AppFormField.confirmedPassword);
debugPrint(
'onSignUpSuccess: ${result.ok}, generatedId: $id, intFormData: $confirmedPassword');
return const Ok<int, AppException>(id);
}
return const Ok(null);
// debugPrint(
// 'onSignUpSuccess: ${result.ok}, generatedId: $id, intFormData: $confirmedPassword');
// return const Ok<int, AppException>(id);
// }
// return const Ok(null);
print('onSignUpWithEmailAndPassword');
return const Ok(1);
}
}
@@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
@@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
@@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
@@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify