doc(auth): update example router
This commit is contained in:
parent
b2a9dac7c6
commit
6802a56bfc
@ -25,7 +25,7 @@ abstract class GetItInitializer {
|
||||
..registerLazySingleton<AuthenticationRemoteDataSource>(
|
||||
() => AuthenticationFirebaseDataSourceImpl(),
|
||||
)
|
||||
..registerLazySingleton<AuthenticationLocalDataSource<int>>(
|
||||
..registerLazySingleton<AuthenticationCacheDataSource<int>>(
|
||||
() => AuthenticationCacheDataSourceImpl<int>(),
|
||||
);
|
||||
|
||||
|
@ -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),
|
||||
),
|
||||
]);
|
||||
}
|
@ -3,33 +3,58 @@
|
||||
// -----
|
||||
// File: app.dart
|
||||
// Created Date: 19/08/2022 12:05:38
|
||||
// Last Modified: Wed Nov 09 2022
|
||||
// Last Modified: Thu Nov 10 2022
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
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/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_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.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_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 {
|
||||
final AuthenticationRepository<int> authenticationRepository =
|
||||
AuthenticationRepositoryImpl(getIt<AuthenticationLocalDataSource<int>>(),
|
||||
getIt<AuthenticationRemoteDataSource>(), (account) async {
|
||||
debugPrint('onSignUpSuccess: $account');
|
||||
return const Ok(null);
|
||||
}, (account) async {
|
||||
debugPrint('onAccountChanges: $account');
|
||||
return const Ok(null);
|
||||
});
|
||||
AuthenticationRepositoryImpl(
|
||||
authenticationCacheDataSource: getIt<AuthenticationCacheDataSource<int>>(),
|
||||
authenticationRemoteDataSource: getIt<AuthenticationRemoteDataSource>(),
|
||||
onSignUpSuccess: onSignUpSuccess,
|
||||
onAuthChange: onAccountChanges,
|
||||
extraSignUpInputs: [
|
||||
FormInput(
|
||||
AppFormField.confirmedPassword,
|
||||
const ConfirmedPassword.pure(),
|
||||
metadata: const FormInputMetadata<void>(export: false),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
App({Key? key}) : super(key: key);
|
||||
|
||||
@ -65,12 +90,12 @@ class App extends StatelessWidget {
|
||||
if (isOnboarding) {
|
||||
return null;
|
||||
} else {
|
||||
return state.namedLocation(WelcomePage.pageName);
|
||||
return '/';
|
||||
}
|
||||
} else {
|
||||
debugPrint('Logged');
|
||||
if (isOnboarding) {
|
||||
return state.namedLocation(HomePage.pageName);
|
||||
return '/home';
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@ -91,13 +116,12 @@ class App extends StatelessWidget {
|
||||
BlocProvider<AuthenticationCubit<int>>.value(
|
||||
value: authenticationCubit,
|
||||
),
|
||||
BlocProvider<SignUpCubit>(
|
||||
BlocProvider<SignUpCubit<int>>(
|
||||
create: (_) => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
formData: Forms.getNormalData(),
|
||||
),
|
||||
),
|
||||
BlocProvider<SignInCubit>(
|
||||
BlocProvider<SignInCubit<int>>(
|
||||
create: (_) => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
@ -106,9 +130,7 @@ class App extends StatelessWidget {
|
||||
child: MaterialApp.router(
|
||||
title: 'Demo Authentication',
|
||||
debugShowCheckedModeBanner: false,
|
||||
routerDelegate: router.routerDelegate,
|
||||
routeInformationParser: router.routeInformationParser,
|
||||
routeInformationProvider: router.routeInformationProvider,
|
||||
routerConfig: router,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
@ -36,7 +36,7 @@ class HomePage extends StatelessWidget {
|
||||
children: [
|
||||
AuthenticationBuilder<int>(
|
||||
authenticated: (context, accountWrapper) =>
|
||||
Text('Logged as ${accountWrapper.account?.email}'),
|
||||
Text('Logged as ${accountWrapper.account?.email} | GeneratedId is ${accountWrapper.data}'),
|
||||
unauthenticated: (context) =>
|
||||
const Text('Not logged (unauthenticated)'),
|
||||
unknown: (context) => const Text('Not logged (unknown)'),
|
||||
|
@ -3,30 +3,31 @@
|
||||
// -----
|
||||
// File: sign_in_form.dart
|
||||
// Created Date: 19/08/2022 15:24:37
|
||||
// Last Modified: Wed Nov 09 2022
|
||||
// Last Modified: Thu Nov 10 2022
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
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 BlocBuilder<SignInCubit, SignInState>(
|
||||
buildWhen: (previous, current) => previous.email != current.email,
|
||||
builder: (context, state) {
|
||||
return InputBuilder<SignInCubit<int>>(
|
||||
field: AuthFormField.email,
|
||||
builder: ((context, cubit, state, field, inputValid) {
|
||||
return TextField(
|
||||
onChanged: (email) => context.read<SignInCubit>().emailChanged(email),
|
||||
onChanged: (email) => cubit.emailChanged(email),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Email',
|
||||
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 {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignInCubit, SignInState>(
|
||||
buildWhen: (previous, current) => previous.password != current.password,
|
||||
builder: (context, state) {
|
||||
return InputBuilder<SignInCubit<int>>(
|
||||
field: AuthFormField.password,
|
||||
builder: ((context, cubit, state, field, inputValid) {
|
||||
return TextField(
|
||||
onChanged: (password) {
|
||||
context.read<SignInCubit>().passwordChanged(password);
|
||||
},
|
||||
onChanged: (pwd) => cubit.passwordChanged(pwd),
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Password',
|
||||
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 {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignInCubit, SignInState>(
|
||||
builder: (context, state) {
|
||||
return state.status.isSubmissionInProgress
|
||||
return SubmitBuilder<SignInCubit<int>>(
|
||||
builder: ((context, cubit, status) {
|
||||
return status.isSubmissionInProgress
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: state.status.isValidated
|
||||
? () =>
|
||||
context.read<SignInCubit>().signInWithEmailAndPassword()
|
||||
: null,
|
||||
onPressed: status.isValidated ? () => cubit.submit() : null,
|
||||
child: const Text('Sign in'),
|
||||
);
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -77,7 +73,7 @@ class SignInForm extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocListener<SignInCubit, SignInState>(
|
||||
return BlocListener<SignInCubit<int>, SignInState>(
|
||||
listener: (context, state) {
|
||||
if (state.status.isSubmissionFailure) {
|
||||
ScaffoldMessenger.of(context)
|
||||
|
@ -3,12 +3,12 @@
|
||||
// -----
|
||||
// File: sign_up_form.dart
|
||||
// Created Date: 19/08/2022 14:41:08
|
||||
// Last Modified: Fri Aug 26 2022
|
||||
// Last Modified: Thu Nov 10 2022
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
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:wyatt_authentication_bloc/wyatt_authentication_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 {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
buildWhen: (previous, current) => previous.email != current.email,
|
||||
builder: (context, state) {
|
||||
return InputBuilder<SignUpCubit<int>>(
|
||||
field: AuthFormField.email,
|
||||
builder: ((context, cubit, state, field, inputValid) {
|
||||
return TextField(
|
||||
onChanged: (email) => context.read<SignUpCubit>().emailChanged(email),
|
||||
onChanged: (email) => cubit.emailChanged(email),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Email',
|
||||
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 {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
buildWhen: (previous, current) => previous.password != current.password,
|
||||
builder: (context, state) {
|
||||
return InputBuilder<SignUpCubit<int>>(
|
||||
field: AuthFormField.password,
|
||||
builder: ((context, cubit, state, field, inputValid) {
|
||||
return TextField(
|
||||
onChanged: (password) {
|
||||
context.read<SignUpCubit>().passwordChanged(password);
|
||||
context.read<SignUpCubit>().dataChanged(
|
||||
AppFormField.confirmedPassword,
|
||||
ConfirmedPassword.dirty(
|
||||
password: password,
|
||||
value: context
|
||||
.read<SignUpCubit>()
|
||||
.state
|
||||
.data
|
||||
.valueOf<String>(
|
||||
AppFormField.confirmedPassword),
|
||||
),
|
||||
);
|
||||
onChanged: (pwd) {
|
||||
cubit.passwordChanged(pwd);
|
||||
cubit.dataChanged(
|
||||
AppFormField.confirmedPassword,
|
||||
ConfirmedPassword.dirty(
|
||||
password: pwd,
|
||||
value: state.form
|
||||
.valueOf<String?>(AppFormField.confirmedPassword)));
|
||||
},
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Password',
|
||||
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 {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
return InputBuilder<SignUpCubit<int>>(
|
||||
field: AppFormField.confirmedPassword,
|
||||
builder: ((context, cubit, state, field, inputValid) {
|
||||
return TextField(
|
||||
onChanged: (confirmPassword) => context
|
||||
.read<SignUpCubit>()
|
||||
.dataChanged(
|
||||
AppFormField.confirmedPassword,
|
||||
ConfirmedPassword.dirty(
|
||||
password: context.read<SignUpCubit>().state.password.value,
|
||||
value: confirmPassword,
|
||||
),
|
||||
),
|
||||
onChanged: (pwd) {
|
||||
cubit.dataChanged(
|
||||
field,
|
||||
ConfirmedPassword.dirty(
|
||||
password:
|
||||
state.form.valueOf<String?>(AuthFormField.password) ?? '',
|
||||
value: pwd),
|
||||
);
|
||||
},
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Confirm password',
|
||||
helperText: '',
|
||||
errorText: state.data.isNotValid(AppFormField.confirmedPassword)
|
||||
? 'Passwords do not match'
|
||||
: null,
|
||||
errorText: !inputValid ? 'Passwords do not match' : null,
|
||||
),
|
||||
);
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -99,17 +92,15 @@ class _ConfirmPasswordInput extends StatelessWidget {
|
||||
class _SignUpButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
return state.status.isSubmissionInProgress
|
||||
return SubmitBuilder<SignUpCubit<int>>(
|
||||
builder: ((context, cubit, status) {
|
||||
return status.isSubmissionInProgress
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: state.status.isValidated
|
||||
? () => context.read<SignUpCubit>().signUpFormSubmitted()
|
||||
: null,
|
||||
onPressed: status.isValidated ? () => cubit.submit() : null,
|
||||
child: const Text('Sign up'),
|
||||
);
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -119,11 +110,9 @@ class SignUpForm extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocListener<SignUpCubit, SignUpState>(
|
||||
return BlocListener<SignUpCubit<int>, SignUpState>(
|
||||
listener: (context, state) {
|
||||
if (state.status.isSubmissionSuccess) {
|
||||
Navigator.of(context).pop();
|
||||
} else if (state.status.isSubmissionFailure) {
|
||||
if (state.status.isSubmissionFailure) {
|
||||
ScaffoldMessenger.of(context)
|
||||
..hideCurrentSnackBar()
|
||||
..showSnackBar(
|
||||
|
@ -41,6 +41,12 @@ dependencies:
|
||||
url: https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages
|
||||
ref: wyatt_form_bloc-v0.0.6
|
||||
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.
|
||||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
|
Loading…
x
Reference in New Issue
Block a user