docs(authentication): update example

This commit is contained in:
Hugo Pointcheval 2023-02-06 21:24:00 +01:00
parent 1c5a6ce9eb
commit 1de922f644
Signed by: hugo
GPG Key ID: 3AAC487E131E00BC
14 changed files with 302 additions and 141 deletions

View File

@ -1,19 +0,0 @@
// 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/>.
abstract class AppFormName {
static const editProfile = 'editProfile';
}

View File

@ -14,9 +14,10 @@
// 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/authentication/sign_in/sign_in_page.dart';
import 'package:example_router/presentation/features/authentication/sign_up/sign_up_page.dart';
import 'package:example_router/presentation/features/edit_account/edit_account_page.dart';
import 'package:example_router/presentation/features/home/home_page.dart';
import 'package:example_router/presentation/features/sign_in/sign_in_page.dart';
import 'package:example_router/presentation/features/sign_up/sign_up_page.dart';
import 'package:example_router/presentation/features/sub/sub_page.dart';
import 'package:example_router/presentation/features/welcome/welcome_page.dart';
import 'package:flutter/cupertino.dart';
@ -82,6 +83,15 @@ class AppRouter {
const SubPage(),
),
),
GoRoute(
path: 'edit_account',
name: EditAccountPage.pageName,
pageBuilder: (context, state) => defaultTransition(
context,
state,
const EditAccountPage(),
),
),
]),
];
}

View File

@ -16,14 +16,13 @@
import 'dart:async';
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:example_router/presentation/features/authentication/sign_in/blocs/sign_in_cubit.dart';
import 'package:example_router/presentation/features/authentication/sign_up/blocs/sign_up_cubit.dart';
import 'package:example_router/presentation/features/edit_account/blocs/edit_account_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
@ -38,7 +37,7 @@ class App extends StatelessWidget {
customPasswordValidator: const CustomPassword.pure(),
extraSignUpInputs: [
FormInput(
AppFormField.confirmedPassword,
AuthFormField.confirmPassword,
const ConfirmedPassword.pure(),
metadata: const FormInputMetadata<void>(export: false),
),
@ -90,9 +89,8 @@ class App extends StatelessWidget {
value: authenticationRepository,
),
RepositoryProvider<FormRepository>(
create: (context) =>
FormRepositoryImpl()..registerForm(AppForms.getEditProfileForm()),
),
create: (context) => FormRepositoryImpl(),
)
],
child: MultiBlocProvider(
providers: [
@ -100,12 +98,17 @@ class App extends StatelessWidget {
value: authenticationCubit,
),
BlocProvider<SignUpCubit<int>>(
create: (_) => CustomSignUpCubit(
create: (_) => ExampleSignUpCubit(
authenticationRepository: authenticationRepository,
),
),
BlocProvider<SignInCubit<int>>(
create: (_) => CustomSignInCubit(
create: (_) => ExampleSignInCubit(
authenticationRepository: authenticationRepository,
),
),
BlocProvider<EditAccountCubit<int>>(
create: (_) => ExampleEditAccountCubit(
authenticationRepository: authenticationRepository,
),
),

View File

@ -14,33 +14,35 @@
// 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_architecture/wyatt_architecture.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';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
class CustomSignInCubit extends SignInCubit<int> {
CustomSignInCubit({
class ExampleSignInCubit extends SignInCubit<int> {
ExampleSignInCubit({
required super.authenticationRepository,
});
@override
FutureOrResult<int?> onSignInWithEmailAndPassword(Result<Account, AppException> result, WyattForm form) {
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) {
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) {
FutureOrResult<int?> onSignInWithGoogle(
Result<Account, AppException> result, WyattForm form) {
print('onSignInWithGoogle');
return const Ok(1);

View File

@ -14,7 +14,7 @@
// 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/sign_in/widgets/sign_in_form.dart';
import 'package:example_router/presentation/features/authentication/sign_in/widgets/sign_in_form.dart';
import 'package:flutter/material.dart';
class SignInPage extends StatelessWidget {

View File

@ -121,21 +121,19 @@ class SignInForm extends StatelessWidget {
..showSnackBar(
SnackBar(content: Text(errorMessage ?? 'Sign In Failure')),
),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
_EmailInput(),
const SizedBox(height: 8),
_PasswordInput(),
const SizedBox(height: 16),
_SignInButton(),
const SizedBox(height: 16),
_SignInAnonymouslyButton(),
const SizedBox(height: 16),
_SignInWithGoogleButton(),
],
),
child: ListView(
shrinkWrap: true,
children: [
_EmailInput(),
const SizedBox(height: 8),
_PasswordInput(),
const SizedBox(height: 16),
_SignInButton(),
const SizedBox(height: 16),
_SignInAnonymouslyButton(),
const SizedBox(height: 16),
_SignInWithGoogleButton(),
],
),
);
}

View File

@ -14,34 +14,21 @@
// 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_form_bloc/wyatt_form_bloc.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
class CustomSignUpCubit extends SignUpCubit<int> {
CustomSignUpCubit({
class ExampleSignUpCubit extends SignUpCubit<int> {
ExampleSignUpCubit({
required super.authenticationRepository,
});
@override
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);
@override
FutureOrResult<int?> onSignUpWithEmailAndPassword(
Result<Account, AppException> result, WyattForm form) async {
print('onSignUpWithEmailAndPassword');
return const Ok(1);
}
}

View File

@ -14,7 +14,7 @@
// 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/sign_up/widgets/sign_up_form.dart';
import 'package:example_router/presentation/features/authentication/sign_up/widgets/sign_up_form.dart';
import 'package:flutter/material.dart';
class SignUpPage extends StatelessWidget {

View File

@ -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/core/constants/form_field.dart';
import 'package:example_router/core/utils/custom_password.dart';
import 'package:flutter/material.dart' hide FormField;
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
@ -51,11 +50,11 @@ class _PasswordInput extends StatelessWidget {
cubit.passwordCustomChanged<CustomPassword>(
CustomPassword.dirty(pwd));
cubit.dataChanged(
AppFormField.confirmedPassword,
AuthFormField.confirmPassword,
ConfirmedPassword.dirty(
password: pwd,
value: state.form
.valueOf<String?>(AppFormField.confirmedPassword)));
.valueOf<String?>(AuthFormField.confirmPassword)));
},
obscureText: true,
decoration: InputDecoration(
@ -73,7 +72,7 @@ class _ConfirmPasswordInput extends StatelessWidget {
@override
Widget build(BuildContext context) {
return InputBuilder<SignUpCubit<int>>(
field: AppFormField.confirmedPassword,
field: AuthFormField.confirmPassword,
builder: ((context, cubit, state, field, input) {
return TextField(
onChanged: (pwd) {
@ -106,7 +105,9 @@ class _SignUpButton extends StatelessWidget {
return status.isSubmissionInProgress
? const CircularProgressIndicator()
: ElevatedButton(
onPressed: status.isValidated ? () => cubit.signUpWithEmailPassword() : null,
onPressed: status.isValidated
? () => cubit.signUpWithEmailPassword()
: null,
child: const Text('Sign up'),
);
}),
@ -126,18 +127,17 @@ class SignUpForm extends StatelessWidget {
..showSnackBar(
SnackBar(content: Text(errorMessage ?? 'Sign Up Failure')),
),
child: SingleChildScrollView(
child: Column(
children: [
_EmailInput(),
const SizedBox(height: 8),
_PasswordInput(),
const SizedBox(height: 8),
_ConfirmPasswordInput(),
const SizedBox(height: 16),
_SignUpButton(),
],
),
child: ListView(
shrinkWrap: true,
children: [
_EmailInput(),
const SizedBox(height: 8),
_PasswordInput(),
const SizedBox(height: 8),
_ConfirmPasswordInput(),
const SizedBox(height: 16),
_SignUpButton(),
],
));
}
}

View File

@ -14,19 +14,27 @@
// 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/constants/form_name.dart';
import 'package:example_router/core/utils/custom_password.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';
abstract class AppForms {
static WyattForm getEditProfileForm() => WyattFormImpl(
[
FormInput(AuthFormField.email, const Email.pure()),
FormInput(AuthFormField.password, const CustomPassword.pure()),
FormInput(AppFormField.oldPassword, const CustomPassword.pure()),
],
name: AppFormName.editProfile,
);
class ExampleEditAccountCubit extends EditAccountCubit<int> {
ExampleEditAccountCubit({required super.authenticationRepository});
@override
FutureOrResult<int?> onEmailUpdated(
Result<Account, AppException> result, WyattForm form) async {
print('onEmailUpdated');
return const Ok(1);
}
@override
FutureOrResult<int?> onPasswordUpdated(
Result<Account, AppException> result, WyattForm form) async {
print('onPasswordUpdated');
return const Ok(1);
}
}

View File

@ -14,7 +14,24 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
abstract class AppFormField {
static const oldPassword = 'oldPassword';
static const confirmedPassword = 'confirmedPassword';
import 'package:example_router/presentation/features/edit_account/widgets/edit_account_form.dart';
import 'package:flutter/material.dart';
class EditAccountPage extends StatelessWidget {
const EditAccountPage({Key? key}) : super(key: key);
static String pageName = 'EditAccount';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Edit Account')),
body: const Padding(
padding: EdgeInsets.all(8),
child: SingleChildScrollView(
child: EditAccountForm(),
),
),
);
}
}

View File

@ -0,0 +1,151 @@
// 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:example_router/core/utils/custom_password.dart';
import 'package:flutter/material.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 InputBuilder<EditAccountCubit<int>>(
field: AuthFormField.email,
builder: ((context, cubit, state, field, input) {
return TextField(
onChanged: (email) => cubit.emailChanged(email),
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email',
helperText: '',
errorText: input.validator.invalid ? 'Invalid email' : null,
),
);
}),
);
}
}
class _PasswordInput extends StatelessWidget {
@override
Widget build(BuildContext context) {
return InputBuilder<EditAccountCubit<int>>(
field: AuthFormField.password,
builder: ((context, cubit, state, field, input) {
return TextField(
onChanged: (pwd) => cubit
.passwordCustomChanged<CustomPassword>(CustomPassword.dirty(pwd)),
obscureText: true,
decoration: InputDecoration(
labelText: 'Password',
helperText: '',
errorText: input.validator.invalid ? 'Invalid password' : null,
),
);
}),
);
}
}
class _EditAccountButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SubmitBuilder<EditAccountCubit<int>>(
builder: ((context, cubit, status) {
return status.isSubmissionInProgress
? const CircularProgressIndicator()
: ElevatedButton(
onPressed: status.isValidated
? () {
cubit.updateEmail();
cubit.updatePassword();
}
: null,
child: const Text('Update Account'),
);
}),
);
}
}
class _EditAccountEmailButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SubmitBuilder<EditAccountCubit<int>>(
builder: ((context, cubit, status) {
return status.isSubmissionInProgress
? const CircularProgressIndicator()
: ElevatedButton(
onPressed: status.isValidated
? () {
cubit.updateEmail();
}
: null,
child: const Text('Update Account Email'),
);
}),
);
}
}
class _EditAccountPasswordButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SubmitBuilder<EditAccountCubit<int>>(
builder: ((context, cubit, status) {
return status.isSubmissionInProgress
? const CircularProgressIndicator()
: ElevatedButton(
onPressed: status.isValidated
? () {
cubit.updatePassword();
}
: null,
child: const Text('Update Account Password'),
);
}),
);
}
}
class EditAccountForm extends StatelessWidget {
const EditAccountForm({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return EditAccountListener<int>(
onError: (context, status, errorMessage) => ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(
SnackBar(content: Text(errorMessage ?? 'Account edition Failure')),
),
child: ListView(
shrinkWrap: true,
children: [
_EmailInput(),
const SizedBox(height: 8),
_PasswordInput(),
const SizedBox(height: 16),
_EditAccountButton(),
const SizedBox(height: 16),
_EditAccountEmailButton(),
const SizedBox(height: 16),
_EditAccountPasswordButton(),
],
),
);
}
}

View File

@ -14,6 +14,7 @@
// 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_account/edit_account_page.dart';
import 'package:example_router/presentation/features/sub/sub_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
@ -29,7 +30,8 @@ class HomePage extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home | ${context.account<AuthenticationCubit<int>, int>()?.email}'),
title: Text(
'Home | ${context.account<AuthenticationCubit<int>, int>()?.email}'),
actions: [
IconButton(
onPressed: () =>
@ -39,29 +41,27 @@ class HomePage extends StatelessWidget {
),
body: Padding(
padding: const EdgeInsets.all(8),
child: SingleChildScrollView(
child: Column(
children: [
AuthenticationBuilder<int>(
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)'),
),
const SizedBox(
height: 8,
),
ElevatedButton(
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'),
// ),
],
),
child: ListView(
children: [
AuthenticationBuilder<int>(
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)'),
),
const SizedBox(
height: 8,
),
ElevatedButton(
onPressed: () => context.pushNamed(SubPage.pageName),
child: const Text('Go to sub page'),
),
ElevatedButton(
onPressed: () => context.pushNamed(EditAccountPage.pageName),
child: const Text('Go to edit account page'),
),
],
),
),
);

View File

@ -14,8 +14,8 @@
// 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/sign_in/sign_in_page.dart';
import 'package:example_router/presentation/features/sign_up/sign_up_page.dart';
import 'package:example_router/presentation/features/authentication/sign_in/sign_in_page.dart';
import 'package:example_router/presentation/features/authentication/sign_up/sign_up_page.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
@ -30,22 +30,26 @@ class WelcomePage extends StatelessWidget {
appBar: AppBar(
title: const Text('Welcome'),
),
body: SingleChildScrollView(
child: Column(
children: [
ElevatedButton(
body: ListView(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () => context.pushNamed(SignUpPage.pageName),
child: const Text('Sign Up')),
ElevatedButton(
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () => context.pushNamed(SignInPage.pageName),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.white),
foregroundColor:
MaterialStateProperty.all<Color>(Colors.blue)),
child: const Text('Sign In'))
],
),
child: const Text('Sign In')),
)
],
),
);
}