doc(auth): update example router

This commit is contained in:
Hugo Pointcheval 2022-11-10 13:31:15 -05:00
parent b2a9dac7c6
commit 6802a56bfc
Signed by: hugo
GPG Key ID: A9E8E9615379254F
7 changed files with 112 additions and 120 deletions

View File

@ -25,7 +25,7 @@ abstract class GetItInitializer {
..registerLazySingleton<AuthenticationRemoteDataSource>( ..registerLazySingleton<AuthenticationRemoteDataSource>(
() => AuthenticationFirebaseDataSourceImpl(), () => AuthenticationFirebaseDataSourceImpl(),
) )
..registerLazySingleton<AuthenticationLocalDataSource<int>>( ..registerLazySingleton<AuthenticationCacheDataSource<int>>(
() => AuthenticationCacheDataSourceImpl<int>(), () => AuthenticationCacheDataSourceImpl<int>(),
); );

View File

@ -1,21 +0,0 @@
// Author: Hugo Pointcheval
// Email: git@pcl.ovh
// -----
// File: forms.dart
// Created Date: 19/08/2022 12:00:31
// Last Modified: 19/08/2022 16:35:52
// -----
// Copyright (c) 2022
import 'package:example_router/core/constants/form_field.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
class Forms {
static FormData getNormalData() => const FormData([
FormInput(
AppFormField.confirmedPassword,
ConfirmedPassword.pure(),
metadata: FormInputMetadata<void>(export: false),
),
]);
}

View File

@ -3,33 +3,58 @@
// ----- // -----
// File: app.dart // File: app.dart
// Created Date: 19/08/2022 12:05:38 // Created Date: 19/08/2022 12:05:38
// Last Modified: Wed Nov 09 2022 // Last Modified: Thu Nov 10 2022
// ----- // -----
// Copyright (c) 2022 // Copyright (c) 2022
import 'dart:async'; 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/dependency_injection/get_it.dart';
import 'package:example_router/core/routes/router.dart'; import 'package:example_router/core/routes/router.dart';
import 'package:example_router/core/utils/forms.dart';
import 'package:example_router/presentation/features/home/home_page.dart';
import 'package:example_router/presentation/features/welcome/welcome_page.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.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'; import 'package:wyatt_type_utils/wyatt_type_utils.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
FutureResult<int?> onSignUpSuccess(
Account? account,
WyattForm form,
) async {
const id = -1;
final confirmedPassword =
form.valueOf<String?>(AppFormField.confirmedPassword);
debugPrint(
'onSignUpSuccess: $account, generatedId: $id, extraFormData: $confirmedPassword');
return const Ok<int, AppException>(id);
}
FutureResult<int?> onAccountChanges(Account? account) async {
final id = Random().nextInt(1000);
debugPrint('onAccountChanges: $account, generatedId: $id');
return Ok<int, AppException>(id);
}
class App extends StatelessWidget { class App extends StatelessWidget {
final AuthenticationRepository<int> authenticationRepository = final AuthenticationRepository<int> authenticationRepository =
AuthenticationRepositoryImpl(getIt<AuthenticationLocalDataSource<int>>(), AuthenticationRepositoryImpl(
getIt<AuthenticationRemoteDataSource>(), (account) async { authenticationCacheDataSource: getIt<AuthenticationCacheDataSource<int>>(),
debugPrint('onSignUpSuccess: $account'); authenticationRemoteDataSource: getIt<AuthenticationRemoteDataSource>(),
return const Ok(null); onSignUpSuccess: onSignUpSuccess,
}, (account) async { onAuthChange: onAccountChanges,
debugPrint('onAccountChanges: $account'); extraSignUpInputs: [
return const Ok(null); FormInput(
}); AppFormField.confirmedPassword,
const ConfirmedPassword.pure(),
metadata: const FormInputMetadata<void>(export: false),
),
],
);
App({Key? key}) : super(key: key); App({Key? key}) : super(key: key);
@ -65,12 +90,12 @@ class App extends StatelessWidget {
if (isOnboarding) { if (isOnboarding) {
return null; return null;
} else { } else {
return state.namedLocation(WelcomePage.pageName); return '/';
} }
} else { } else {
debugPrint('Logged'); debugPrint('Logged');
if (isOnboarding) { if (isOnboarding) {
return state.namedLocation(HomePage.pageName); return '/home';
} else { } else {
return null; return null;
} }
@ -91,13 +116,12 @@ class App extends StatelessWidget {
BlocProvider<AuthenticationCubit<int>>.value( BlocProvider<AuthenticationCubit<int>>.value(
value: authenticationCubit, value: authenticationCubit,
), ),
BlocProvider<SignUpCubit>( BlocProvider<SignUpCubit<int>>(
create: (_) => SignUpCubit( create: (_) => SignUpCubit(
authenticationRepository: authenticationRepository, authenticationRepository: authenticationRepository,
formData: Forms.getNormalData(),
), ),
), ),
BlocProvider<SignInCubit>( BlocProvider<SignInCubit<int>>(
create: (_) => SignInCubit( create: (_) => SignInCubit(
authenticationRepository: authenticationRepository, authenticationRepository: authenticationRepository,
), ),
@ -106,9 +130,7 @@ class App extends StatelessWidget {
child: MaterialApp.router( child: MaterialApp.router(
title: 'Demo Authentication', title: 'Demo Authentication',
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
routerDelegate: router.routerDelegate, routerConfig: router,
routeInformationParser: router.routeInformationParser,
routeInformationProvider: router.routeInformationProvider,
), ),
), ),
); );

View File

@ -36,7 +36,7 @@ class HomePage extends StatelessWidget {
children: [ children: [
AuthenticationBuilder<int>( AuthenticationBuilder<int>(
authenticated: (context, accountWrapper) => authenticated: (context, accountWrapper) =>
Text('Logged as ${accountWrapper.account?.email}'), Text('Logged as ${accountWrapper.account?.email} | GeneratedId is ${accountWrapper.data}'),
unauthenticated: (context) => unauthenticated: (context) =>
const Text('Not logged (unauthenticated)'), const Text('Not logged (unauthenticated)'),
unknown: (context) => const Text('Not logged (unknown)'), unknown: (context) => const Text('Not logged (unknown)'),

View File

@ -3,30 +3,31 @@
// ----- // -----
// File: sign_in_form.dart // File: sign_in_form.dart
// Created Date: 19/08/2022 15:24:37 // Created Date: 19/08/2022 15:24:37
// Last Modified: Wed Nov 09 2022 // Last Modified: Thu Nov 10 2022
// ----- // -----
// Copyright (c) 2022 // Copyright (c) 2022
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart'; import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
class _EmailInput extends StatelessWidget { class _EmailInput extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<SignInCubit, SignInState>( return InputBuilder<SignInCubit<int>>(
buildWhen: (previous, current) => previous.email != current.email, field: AuthFormField.email,
builder: (context, state) { builder: ((context, cubit, state, field, inputValid) {
return TextField( return TextField(
onChanged: (email) => context.read<SignInCubit>().emailChanged(email), onChanged: (email) => cubit.emailChanged(email),
keyboardType: TextInputType.emailAddress, keyboardType: TextInputType.emailAddress,
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Email', labelText: 'Email',
helperText: '', helperText: '',
errorText: state.email.invalid ? 'Invalid email' : null, errorText: !inputValid ? 'Invalid email' : null,
), ),
); );
}, }),
); );
} }
} }
@ -34,21 +35,19 @@ class _EmailInput extends StatelessWidget {
class _PasswordInput extends StatelessWidget { class _PasswordInput extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<SignInCubit, SignInState>( return InputBuilder<SignInCubit<int>>(
buildWhen: (previous, current) => previous.password != current.password, field: AuthFormField.password,
builder: (context, state) { builder: ((context, cubit, state, field, inputValid) {
return TextField( return TextField(
onChanged: (password) { onChanged: (pwd) => cubit.passwordChanged(pwd),
context.read<SignInCubit>().passwordChanged(password);
},
obscureText: true, obscureText: true,
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Password', labelText: 'Password',
helperText: '', helperText: '',
errorText: state.password.invalid ? 'Invalid password' : null, errorText: !inputValid ? 'Invalid password' : null,
), ),
); );
}, }),
); );
} }
} }
@ -56,18 +55,15 @@ class _PasswordInput extends StatelessWidget {
class _SignInButton extends StatelessWidget { class _SignInButton extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<SignInCubit, SignInState>( return SubmitBuilder<SignInCubit<int>>(
builder: (context, state) { builder: ((context, cubit, status) {
return state.status.isSubmissionInProgress return status.isSubmissionInProgress
? const CircularProgressIndicator() ? const CircularProgressIndicator()
: ElevatedButton( : ElevatedButton(
onPressed: state.status.isValidated onPressed: status.isValidated ? () => cubit.submit() : null,
? () =>
context.read<SignInCubit>().signInWithEmailAndPassword()
: null,
child: const Text('Sign in'), child: const Text('Sign in'),
); );
}, }),
); );
} }
} }
@ -77,7 +73,7 @@ class SignInForm extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocListener<SignInCubit, SignInState>( return BlocListener<SignInCubit<int>, SignInState>(
listener: (context, state) { listener: (context, state) {
if (state.status.isSubmissionFailure) { if (state.status.isSubmissionFailure) {
ScaffoldMessenger.of(context) ScaffoldMessenger.of(context)

View File

@ -3,12 +3,12 @@
// ----- // -----
// File: sign_up_form.dart // File: sign_up_form.dart
// Created Date: 19/08/2022 14:41:08 // Created Date: 19/08/2022 14:41:08
// Last Modified: Fri Aug 26 2022 // Last Modified: Thu Nov 10 2022
// ----- // -----
// Copyright (c) 2022 // Copyright (c) 2022
import 'package:example_router/core/constants/form_field.dart'; import 'package:example_router/core/constants/form_field.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart' hide FormField;
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart'; import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart'; import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
@ -16,19 +16,19 @@ import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
class _EmailInput extends StatelessWidget { class _EmailInput extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<SignUpCubit, SignUpState>( return InputBuilder<SignUpCubit<int>>(
buildWhen: (previous, current) => previous.email != current.email, field: AuthFormField.email,
builder: (context, state) { builder: ((context, cubit, state, field, inputValid) {
return TextField( return TextField(
onChanged: (email) => context.read<SignUpCubit>().emailChanged(email), onChanged: (email) => cubit.emailChanged(email),
keyboardType: TextInputType.emailAddress, keyboardType: TextInputType.emailAddress,
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Email', labelText: 'Email',
helperText: '', helperText: '',
errorText: state.email.invalid ? 'Invalid email' : null, errorText: !inputValid ? 'Invalid email' : null,
), ),
); );
}, }),
); );
} }
} }
@ -36,33 +36,27 @@ class _EmailInput extends StatelessWidget {
class _PasswordInput extends StatelessWidget { class _PasswordInput extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<SignUpCubit, SignUpState>( return InputBuilder<SignUpCubit<int>>(
buildWhen: (previous, current) => previous.password != current.password, field: AuthFormField.password,
builder: (context, state) { builder: ((context, cubit, state, field, inputValid) {
return TextField( return TextField(
onChanged: (password) { onChanged: (pwd) {
context.read<SignUpCubit>().passwordChanged(password); cubit.passwordChanged(pwd);
context.read<SignUpCubit>().dataChanged( cubit.dataChanged(
AppFormField.confirmedPassword, AppFormField.confirmedPassword,
ConfirmedPassword.dirty( ConfirmedPassword.dirty(
password: password, password: pwd,
value: context value: state.form
.read<SignUpCubit>() .valueOf<String?>(AppFormField.confirmedPassword)));
.state
.data
.valueOf<String>(
AppFormField.confirmedPassword),
),
);
}, },
obscureText: true, obscureText: true,
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Password', labelText: 'Password',
helperText: '', helperText: '',
errorText: state.password.invalid ? 'Invalid password' : null, errorText: !inputValid ? 'Invalid password' : null,
), ),
); );
}, }),
); );
} }
} }
@ -70,28 +64,27 @@ class _PasswordInput extends StatelessWidget {
class _ConfirmPasswordInput extends StatelessWidget { class _ConfirmPasswordInput extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<SignUpCubit, SignUpState>( return InputBuilder<SignUpCubit<int>>(
builder: (context, state) { field: AppFormField.confirmedPassword,
builder: ((context, cubit, state, field, inputValid) {
return TextField( return TextField(
onChanged: (confirmPassword) => context onChanged: (pwd) {
.read<SignUpCubit>() cubit.dataChanged(
.dataChanged( field,
AppFormField.confirmedPassword, ConfirmedPassword.dirty(
ConfirmedPassword.dirty( password:
password: context.read<SignUpCubit>().state.password.value, state.form.valueOf<String?>(AuthFormField.password) ?? '',
value: confirmPassword, value: pwd),
), );
), },
obscureText: true, obscureText: true,
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Confirm password', labelText: 'Confirm password',
helperText: '', helperText: '',
errorText: state.data.isNotValid(AppFormField.confirmedPassword) errorText: !inputValid ? 'Passwords do not match' : null,
? 'Passwords do not match'
: null,
), ),
); );
}, }),
); );
} }
} }
@ -99,17 +92,15 @@ class _ConfirmPasswordInput extends StatelessWidget {
class _SignUpButton extends StatelessWidget { class _SignUpButton extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<SignUpCubit, SignUpState>( return SubmitBuilder<SignUpCubit<int>>(
builder: (context, state) { builder: ((context, cubit, status) {
return state.status.isSubmissionInProgress return status.isSubmissionInProgress
? const CircularProgressIndicator() ? const CircularProgressIndicator()
: ElevatedButton( : ElevatedButton(
onPressed: state.status.isValidated onPressed: status.isValidated ? () => cubit.submit() : null,
? () => context.read<SignUpCubit>().signUpFormSubmitted()
: null,
child: const Text('Sign up'), child: const Text('Sign up'),
); );
}, }),
); );
} }
} }
@ -119,11 +110,9 @@ class SignUpForm extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocListener<SignUpCubit, SignUpState>( return BlocListener<SignUpCubit<int>, SignUpState>(
listener: (context, state) { listener: (context, state) {
if (state.status.isSubmissionSuccess) { if (state.status.isSubmissionFailure) {
Navigator.of(context).pop();
} else if (state.status.isSubmissionFailure) {
ScaffoldMessenger.of(context) ScaffoldMessenger.of(context)
..hideCurrentSnackBar() ..hideCurrentSnackBar()
..showSnackBar( ..showSnackBar(

View File

@ -41,6 +41,12 @@ dependencies:
url: https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages url: https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages
ref: wyatt_form_bloc-v0.0.6 ref: wyatt_form_bloc-v0.0.6
path: packages/wyatt_form_bloc path: packages/wyatt_form_bloc
wyatt_type_utils:
git:
url: https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages
ref: wyatt_type_utils-v0.0.3+1
path: packages/wyatt_type_utils
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.