chore(auth): remove old example and rename actual
This commit is contained in:
@@ -1,182 +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 'dart:developer';
|
||||
|
||||
import 'package:authentication_bloc_example/constants.dart';
|
||||
import 'package:authentication_bloc_example/home/home_page.dart';
|
||||
import 'package:authentication_bloc_example/login/login_page.dart';
|
||||
import 'package:authentication_bloc_example/model.dart';
|
||||
import 'package:cloud_firestore/cloud_firestore.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 App extends StatelessWidget {
|
||||
const App({Key? key}) : super(key: key);
|
||||
|
||||
static FormData getNormalFormData() {
|
||||
return const FormData([
|
||||
FormInput(formFieldName, Name.pure()),
|
||||
FormInput(formFieldPhone, Phone.pure()),
|
||||
FormInput(formFieldPro, Boolean.pure()),
|
||||
FormInput(
|
||||
formFieldConfirmedPassword,
|
||||
ConfirmedPassword.pure(),
|
||||
metadata: FormInputMetadata(export: false),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
static FormData getProFormData() {
|
||||
return const FormData([
|
||||
FormInput(formFieldName, Name.pure()),
|
||||
FormInput(formFieldPhone, Phone.pure()),
|
||||
FormInput(formFieldPro, Boolean.pure()),
|
||||
FormInput(formFieldSiren, Siren.pure()),
|
||||
FormInput(formFieldIban, Iban.pure()),
|
||||
FormInput(
|
||||
formFieldConfirmedPassword,
|
||||
ConfirmedPassword.pure(),
|
||||
metadata: FormInputMetadata(export: false),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
// On Authentication Success. (Authenticated or Anonymous)
|
||||
// User callback.
|
||||
Future<Map<String, dynamic>> onAuthSuccess(UserInterface user) async {
|
||||
if (user.isNotEmpty && !user.isAnonymous) {
|
||||
// Check if user is register in Firesore.
|
||||
DocumentSnapshot firestoreUser = await FirebaseFirestore.instance
|
||||
.collection(firestoreCollectionUsers)
|
||||
.doc(user.uid)
|
||||
.get();
|
||||
|
||||
if (!firestoreUser.exists) {
|
||||
// Register user in Firestore when sign in with social account.
|
||||
final uid = user.uid;
|
||||
final u = {'uid': uid, 'email': user.email};
|
||||
await FirebaseFirestore.instance
|
||||
.collection(firestoreCollectionUsers)
|
||||
.doc(uid)
|
||||
.set(u);
|
||||
return {
|
||||
'user': UserFirestore(
|
||||
uid: uid,
|
||||
email: user.email ?? '',
|
||||
name: user.displayName ?? '',
|
||||
phone: user.phoneNumber ?? ''),
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
'user': UserFirestore.fromMap(
|
||||
firestoreUser.data() as Map<String, dynamic>),
|
||||
...firestoreUser.data() as Map<String, dynamic>? ?? {}
|
||||
};
|
||||
}
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
// On Sign Up Success.
|
||||
Future<void> onSignUpSuccess(SignUpState state, String? uid) async {
|
||||
if (uid != null) {
|
||||
final data = state.data.toMap();
|
||||
final user = {'uid': uid, 'email': state.email.value, ...data};
|
||||
log('onSignUpSuccess: $user');
|
||||
await FirebaseFirestore.instance
|
||||
.collection(firestoreCollectionUsers)
|
||||
.doc(uid)
|
||||
.set(user);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
AuthenticationRepositoryInterface authenticationRepository =
|
||||
AuthenticationRepositoryFirebase();
|
||||
|
||||
AuthenticationCubit authenticationCubit = AuthenticationCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
onAuthSuccess: onAuthSuccess,
|
||||
);
|
||||
|
||||
SignUpCubit signUpCubit = SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
entries: getNormalFormData(),
|
||||
onSignUpSuccess: onSignUpSuccess,
|
||||
);
|
||||
|
||||
SignInCubit signInCubit = SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
authenticationCubit: authenticationCubit,
|
||||
);
|
||||
|
||||
return MultiRepositoryProvider(
|
||||
providers: [
|
||||
RepositoryProvider<AuthenticationRepositoryInterface>(
|
||||
create: (context) => authenticationRepository,
|
||||
),
|
||||
],
|
||||
child: MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider<AuthenticationCubit>(
|
||||
create: (context) => authenticationCubit..init(),
|
||||
),
|
||||
BlocProvider<SignUpCubit>(
|
||||
create: (context) => signUpCubit,
|
||||
),
|
||||
BlocProvider<SignInCubit>(
|
||||
create: (context) => signInCubit,
|
||||
),
|
||||
],
|
||||
child: const AppView(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AppView extends StatelessWidget {
|
||||
const AppView({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
buttonTheme: const ButtonThemeData(
|
||||
buttonColor: Colors.blue,
|
||||
textTheme: ButtonTextTheme.primary,
|
||||
),
|
||||
),
|
||||
home: AuthenticationBuilder(
|
||||
unknown: (context) {
|
||||
return const Scaffold(
|
||||
body: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
);
|
||||
},
|
||||
authenticated: (context, user, userData) => const HomePage(),
|
||||
unauthenticated: (context) => const LoginPage(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,44 +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:bloc/bloc.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class AppBlocObserver extends BlocObserver {
|
||||
@override
|
||||
void onEvent(Bloc bloc, Object? event) {
|
||||
super.onEvent(bloc, event);
|
||||
debugPrint(event.toString());
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
|
||||
debugPrint(error.toString());
|
||||
super.onError(bloc, error, stackTrace);
|
||||
}
|
||||
|
||||
@override
|
||||
void onChange(BlocBase bloc, Change change) {
|
||||
super.onChange(bloc, change);
|
||||
debugPrint('curr:\t${change.currentState}\nnext:\t${change.nextState}');
|
||||
}
|
||||
|
||||
@override
|
||||
void onTransition(Bloc bloc, Transition transition) {
|
||||
super.onTransition(bloc, transition);
|
||||
debugPrint(transition.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: bootstrap.dart
|
||||
// Created Date: 19/08/2022 15:05:17
|
||||
// Last Modified: Fri Nov 11 2022
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:example_router/core/dependency_injection/get_it.dart';
|
||||
import 'package:example_router/core/utils/app_bloc_observer.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
Future<void> bootstrap(FutureOr<Widget> Function() builder) async {
|
||||
await runZonedGuarded(
|
||||
() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
Bloc.observer = AppBlocObserver();
|
||||
|
||||
FlutterError.onError = (details) {
|
||||
debugPrint(details.toString());
|
||||
};
|
||||
await GetItInitializer.init();
|
||||
|
||||
runApp(await builder());
|
||||
},
|
||||
(error, stackTrace) => debugPrint(error.toString()),
|
||||
);
|
||||
}
|
||||
@@ -1,24 +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/>.
|
||||
|
||||
const String formFieldName = 'name';
|
||||
const String formFieldPhone = 'phone';
|
||||
const String formFieldPro = 'isPro';
|
||||
const String formFieldConfirmedPassword = 'confirmedPassword';
|
||||
const String formFieldSiren = 'siren';
|
||||
const String formFieldIban = 'iban';
|
||||
|
||||
const String firestoreCollectionUsers = 'authentication_bloc_users';
|
||||
@@ -0,0 +1,12 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: form_field.dart
|
||||
// Created Date: 19/08/2022 11:52:33
|
||||
// Last Modified: 19/08/2022 16:35:39
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
abstract class AppFormField {
|
||||
static const confirmedPassword = 'confirmedPassword';
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// 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:get_it/get_it.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
|
||||
|
||||
final getIt = GetIt.I;
|
||||
|
||||
abstract class GetItInitializer {
|
||||
static Future<void> init() async {
|
||||
getIt
|
||||
..registerLazySingleton<AuthenticationRemoteDataSource>(
|
||||
() => AuthenticationMockDataSourceImpl(registeredAccounts: [
|
||||
Pair(
|
||||
AccountModel(
|
||||
uid: '1',
|
||||
emailVerified: true,
|
||||
isAnonymous: false,
|
||||
providerId: 'wyatt',
|
||||
email: 'toto@test.fr',
|
||||
),
|
||||
'toto1234',
|
||||
),
|
||||
Pair(
|
||||
AccountModel(
|
||||
uid: '2',
|
||||
emailVerified: false,
|
||||
isAnonymous: false,
|
||||
providerId: 'wyatt',
|
||||
email: 'tata@test.fr',
|
||||
),
|
||||
'tata1234',
|
||||
),
|
||||
]),
|
||||
)
|
||||
..registerLazySingleton<AuthenticationCacheDataSource<int>>(
|
||||
() => AuthenticationCacheDataSourceImpl<int>(),
|
||||
);
|
||||
|
||||
await getIt.allReady();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: router.dart
|
||||
// Created Date: 19/08/2022 11:52:22
|
||||
// Last Modified: 19/08/2022 16:39:07
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
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';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class AppRouter {
|
||||
/// Default transition for all pages
|
||||
static Page<void> defaultTransition(
|
||||
BuildContext context,
|
||||
GoRouterState state,
|
||||
Widget child,
|
||||
) =>
|
||||
CupertinoPage<void>(
|
||||
key: state.pageKey,
|
||||
child: child,
|
||||
);
|
||||
|
||||
static final publicRoutes = ['/', '/sign_in', '/sign_up'];
|
||||
|
||||
static final List<GoRoute> routes = [
|
||||
GoRoute(
|
||||
path: '/',
|
||||
name: WelcomePage.pageName,
|
||||
pageBuilder: (context, state) => defaultTransition(
|
||||
context,
|
||||
state,
|
||||
const WelcomePage(),
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/sign_in',
|
||||
name: SignInPage.pageName,
|
||||
pageBuilder: (context, state) => defaultTransition(
|
||||
context,
|
||||
state,
|
||||
const SignInPage(),
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/sign_up',
|
||||
name: SignUpPage.pageName,
|
||||
pageBuilder: (context, state) => defaultTransition(
|
||||
context,
|
||||
state,
|
||||
const SignUpPage(),
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/home',
|
||||
name: HomePage.pageName,
|
||||
pageBuilder: (context, state) => defaultTransition(
|
||||
context,
|
||||
state,
|
||||
const HomePage(),
|
||||
),
|
||||
),
|
||||
GoRoute(
|
||||
path: '/home/sub',
|
||||
name: SubPage.pageName,
|
||||
pageBuilder: (context, state) => defaultTransition(
|
||||
context,
|
||||
state,
|
||||
const SubPage(),
|
||||
),
|
||||
),
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: app_bloc_observer.dart
|
||||
// Created Date: 19/08/2022 12:02:23
|
||||
// Last Modified: 19/08/2022 12:02:45
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class AppBlocObserver extends BlocObserver {
|
||||
@override
|
||||
void onEvent(Bloc bloc, Object? event) {
|
||||
super.onEvent(bloc, event);
|
||||
debugPrint(event.toString());
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
|
||||
debugPrint(error.toString());
|
||||
super.onError(bloc, error, stackTrace);
|
||||
}
|
||||
|
||||
@override
|
||||
void onChange(BlocBase bloc, Change change) {
|
||||
super.onChange(bloc, change);
|
||||
debugPrint('curr:\t${change.currentState}\nnext:\t${change.nextState}');
|
||||
}
|
||||
|
||||
@override
|
||||
void onTransition(Bloc bloc, Transition transition) {
|
||||
super.onTransition(bloc, transition);
|
||||
debugPrint(transition.toString());
|
||||
}
|
||||
}
|
||||
-44
@@ -1,44 +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:authentication_bloc_example/forgot_password/widgets/forgot_password_form.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class ForgotPasswordPage extends StatelessWidget {
|
||||
const ForgotPasswordPage({Key? key}) : super(key: key);
|
||||
|
||||
static Route route() {
|
||||
return MaterialPageRoute<void>(builder: (_) => const ForgotPasswordPage());
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Forgot Password')),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: BlocProvider(
|
||||
create: (_) => PasswordResetCubit(
|
||||
context.read<AuthenticationRepositoryInterface>(),
|
||||
),
|
||||
child: const ForgotPasswordForm(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
-92
@@ -1,92 +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: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<PasswordResetCubit, PasswordResetState>(
|
||||
buildWhen: (previous, current) => previous.email != current.email,
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (email) =>
|
||||
context.read<PasswordResetCubit>().emailChanged(email),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'email',
|
||||
helperText: '',
|
||||
errorText: state.email.invalid ? 'invalid email' : null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ResetButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<PasswordResetCubit, PasswordResetState>(
|
||||
buildWhen: (previous, current) => previous.status != current.status,
|
||||
builder: (context, state) {
|
||||
return state.status.isSubmissionInProgress
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: state.status.isValidated
|
||||
? () => context
|
||||
.read<PasswordResetCubit>()
|
||||
.sendPasswordResetEmail()
|
||||
: null,
|
||||
child: const Text('SEND EMAIL'),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ForgotPasswordForm extends StatelessWidget {
|
||||
const ForgotPasswordForm({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocListener<PasswordResetCubit, PasswordResetState>(
|
||||
listener: (context, state) {
|
||||
if (state.status.isSubmissionSuccess) {
|
||||
Navigator.of(context).pop();
|
||||
} else if (state.status.isSubmissionFailure) {
|
||||
ScaffoldMessenger.of(context)
|
||||
..hideCurrentSnackBar()
|
||||
..showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(state.errorMessage ?? 'Password reset Failure'),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: Align(
|
||||
alignment: const Alignment(0, -1 / 3),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [_EmailInput(), const SizedBox(height: 8), _ResetButton()],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,65 +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:authentication_bloc_example/home/widgets/infos.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import 'package:authentication_bloc_example/home/widgets/email_verification.dart';
|
||||
|
||||
class HomePage extends StatelessWidget {
|
||||
const HomePage({Key? key}) : super(key: key);
|
||||
|
||||
static Route route() {
|
||||
return MaterialPageRoute<void>(builder: (_) => const HomePage());
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final user = context.select((AuthenticationCubit cubit) => cubit.state.user);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Home'),
|
||||
actions: <Widget>[
|
||||
IconButton(
|
||||
icon: const Icon(Icons.exit_to_app),
|
||||
onPressed: () => context
|
||||
.read<AuthenticationCubit>()
|
||||
.logOut(),
|
||||
)
|
||||
],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: BlocProvider(
|
||||
create: (_) => EmailVerificationCubit(
|
||||
context.read<AuthenticationRepositoryInterface>(),
|
||||
)..checkEmailVerification(),
|
||||
child: BlocBuilder<EmailVerificationCubit, EmailVerificationState>(
|
||||
builder: (context, state) {
|
||||
if (state.isVerified || user!.isAnonymous) {
|
||||
return const UserInfo();
|
||||
} else {
|
||||
return const EmailVerification();
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,37 +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:flutter/material.dart';
|
||||
|
||||
const _avatarSize = 48.0;
|
||||
|
||||
class Avatar extends StatelessWidget {
|
||||
const Avatar({Key? key, this.photo}) : super(key: key);
|
||||
|
||||
final String? photo;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final photo = this.photo;
|
||||
return CircleAvatar(
|
||||
radius: _avatarSize,
|
||||
backgroundImage: photo != null ? NetworkImage(photo) : null,
|
||||
child: photo == null
|
||||
? const Icon(Icons.person_outline, size: _avatarSize)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,53 +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:flutter/material.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class EmailVerification extends StatelessWidget {
|
||||
const EmailVerification({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final user = context.select((AuthenticationCubit cubit) => cubit.state.user);
|
||||
final userData = context.select((AuthenticationCubit cubit) => cubit.state.userData);
|
||||
return Align(
|
||||
alignment: const Alignment(0, -1 / 3),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Text("Hello ${userData!['name'] ?? 'null'}!"),
|
||||
const SizedBox(height: 4),
|
||||
Text("Email '${user?.email ?? 'null'}' is not verified"),
|
||||
const SizedBox(height: 4),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<EmailVerificationCubit>().sendEmailVerification();
|
||||
},
|
||||
child: const Text('(Re)send email'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<EmailVerificationCubit>().checkEmailVerification();
|
||||
},
|
||||
child: const Text('Refresh'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,58 +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:authentication_bloc_example/home/widgets/avatar.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:authentication_bloc_example/constants.dart';
|
||||
|
||||
class UserInfo extends StatelessWidget {
|
||||
const UserInfo({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final textTheme = Theme.of(context).textTheme;
|
||||
final user = context.select((AuthenticationCubit cubit) => cubit.state.user);
|
||||
final userData = context.select((AuthenticationCubit cubit) => cubit.state.userData);
|
||||
return Align(
|
||||
alignment: const Alignment(0, -1 / 3),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Avatar(photo: user?.photoURL),
|
||||
const SizedBox(height: 4),
|
||||
Text("Email: ${user?.email ?? 'null'}", style: textTheme.headline6),
|
||||
const SizedBox(height: 4),
|
||||
Text("Name: ${userData![formFieldName] ?? 'null'}",
|
||||
style: textTheme.headline6),
|
||||
const SizedBox(height: 4),
|
||||
Text("Phone: ${userData[formFieldPhone] ?? 'null'}",
|
||||
style: textTheme.headline6),
|
||||
const SizedBox(height: 4),
|
||||
Text("IsPro: ${userData[formFieldPro] ?? 'null'}",
|
||||
style: textTheme.headline6),
|
||||
const SizedBox(height: 4),
|
||||
Text("IsAnonymous: ${user?.isAnonymous.toString() ?? 'null'}",
|
||||
style: textTheme.headline6),
|
||||
const SizedBox(height: 4),
|
||||
Text("IsEmailVerified: ${user?.emailVerified.toString() ?? 'null'}",
|
||||
style: textTheme.headline6),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,37 +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:authentication_bloc_example/login/widgets/login_form.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LoginPage extends StatelessWidget {
|
||||
const LoginPage({Key? key}) : super(key: key);
|
||||
|
||||
static Route route() {
|
||||
return MaterialPageRoute<void>(builder: (_) => const LoginPage());
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Login')),
|
||||
body: const Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
child: LoginForm(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,214 +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:authentication_bloc_example/app/app.dart';
|
||||
import 'package:authentication_bloc_example/constants.dart';
|
||||
import 'package:authentication_bloc_example/forgot_password/forgot_password_page.dart';
|
||||
import 'package:authentication_bloc_example/sign_up/sign_up_page.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 BlocBuilder<SignInCubit, SignInState>(
|
||||
buildWhen: (previous, current) => previous.email != current.email,
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (email) => context.read<SignInCubit>().emailChanged(email),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'email',
|
||||
helperText: '',
|
||||
errorText: state.email.invalid ? 'invalid email' : null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PasswordInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignInCubit, SignInState>(
|
||||
buildWhen: (previous, current) => previous.password != current.password,
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (password) =>
|
||||
context.read<SignInCubit>().passwordChanged(password),
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'password',
|
||||
helperText: '',
|
||||
errorText: state.password.invalid ? 'invalid password' : null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _LoginAnonButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignInCubit, SignInState>(
|
||||
buildWhen: (previous, current) => previous.status != current.status,
|
||||
builder: (context, state) {
|
||||
return state.status.isSubmissionInProgress
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: () =>
|
||||
context.read<SignInCubit>().signInAnonymously(),
|
||||
child: const Text('LOGIN ANONYMOUSLY'),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _LoginWithPasswordButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignInCubit, SignInState>(
|
||||
buildWhen: (previous, current) => previous.status != current.status,
|
||||
builder: (context, state) {
|
||||
return state.status.isSubmissionInProgress
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: state.status.isValidated
|
||||
? () =>
|
||||
context.read<SignInCubit>().signInWithEmailAndPassword()
|
||||
: null,
|
||||
child: const Text('LOGIN WITH PASSWORD'),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _LoginWithGoogleButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignInCubit, SignInState>(
|
||||
buildWhen: (previous, current) => previous.status != current.status,
|
||||
builder: (context, state) {
|
||||
return state.status.isSubmissionInProgress
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: () =>
|
||||
context.read<SignInCubit>().signInWithGoogle(),
|
||||
child: const Text('LOGIN GOOGLE'),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SignUpButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return TextButton(
|
||||
onPressed: () {
|
||||
context.read<SignUpCubit>().updateFormData(App.getNormalFormData());
|
||||
Navigator.of(context).push<void>(SignUpPage.route());
|
||||
},
|
||||
child: Text(
|
||||
'CREATE ACCOUNT',
|
||||
style: TextStyle(color: theme.primaryColor),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SignUpAsProButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return TextButton(
|
||||
onPressed: () {
|
||||
context.read<SignUpCubit>().updateFormData(App.getProFormData());
|
||||
context.read<SignUpCubit>().dataChanged(
|
||||
formFieldPro,
|
||||
const Boolean.dirty(value: true),
|
||||
);
|
||||
Navigator.of(context).push<void>(SignUpPage.route());
|
||||
},
|
||||
child: Text(
|
||||
'CREATE PRO ACCOUNT',
|
||||
style: TextStyle(color: theme.primaryColor),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class LoginForm extends StatelessWidget {
|
||||
const LoginForm({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocListener<SignInCubit, SignInState>(
|
||||
listener: (context, state) {
|
||||
if (state.status.isSubmissionFailure) {
|
||||
ScaffoldMessenger.of(context)
|
||||
..hideCurrentSnackBar()
|
||||
..showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(state.errorMessage ?? 'Authentication Failure'),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: Align(
|
||||
alignment: const Alignment(0, -1 / 3),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const SizedBox(height: 120),
|
||||
_EmailInput(),
|
||||
const SizedBox(height: 8),
|
||||
_PasswordInput(),
|
||||
const SizedBox(height: 8),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
Navigator.of(context).push(ForgotPasswordPage.route());
|
||||
},
|
||||
child: Text(
|
||||
'Forgot password ?',
|
||||
style: Theme.of(context).textTheme.bodyText2,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
_LoginWithPasswordButton(),
|
||||
const SizedBox(height: 8),
|
||||
_LoginAnonButton(),
|
||||
const SizedBox(height: 8),
|
||||
_LoginWithGoogleButton(),
|
||||
const SizedBox(height: 8),
|
||||
_SignUpButton(),
|
||||
const SizedBox(height: 8),
|
||||
_SignUpAsProButton(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,35 +1,6 @@
|
||||
// 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/bootstrap.dart';
|
||||
import 'package:example_router/presentation/features/app/app.dart';
|
||||
|
||||
import 'package:authentication_bloc_example/app/app.dart';
|
||||
import 'package:authentication_bloc_example/app/bloc_observer.dart';
|
||||
import 'package:authentication_bloc_example/firebase_options.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
);
|
||||
BlocOverrides.runZoned(
|
||||
() => runApp(
|
||||
const App(),
|
||||
),
|
||||
blocObserver: AppBlocObserver(),
|
||||
);
|
||||
void main() {
|
||||
bootstrap(App.new);
|
||||
}
|
||||
|
||||
@@ -1,52 +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/>.
|
||||
|
||||
class UserFirestore {
|
||||
final String email;
|
||||
final String name;
|
||||
final String phone;
|
||||
final String uid;
|
||||
|
||||
UserFirestore({
|
||||
required this.email,
|
||||
required this.name,
|
||||
required this.phone,
|
||||
required this.uid,
|
||||
});
|
||||
|
||||
factory UserFirestore.fromMap(Map<String, dynamic> map) {
|
||||
return UserFirestore(
|
||||
uid: map['uid'],
|
||||
email: map['email'],
|
||||
name: map['name'],
|
||||
phone: map['phone'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'uid': uid,
|
||||
'email': email,
|
||||
'name': name,
|
||||
'phone': phone,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'UserFirestore(email: $email, name: $name, phone: $phone, uid: $uid)';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: app.dart
|
||||
// Created Date: 19/08/2022 12:05:38
|
||||
// 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: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(
|
||||
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);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
AuthenticationState? previous;
|
||||
|
||||
final AuthenticationCubit<int> authenticationCubit =
|
||||
AuthenticationCubit(authenticationRepository: authenticationRepository);
|
||||
|
||||
final GoRouter router = GoRouter(
|
||||
initialLocation: '/',
|
||||
routes: AppRouter.routes,
|
||||
debugLogDiagnostics: true,
|
||||
errorBuilder: (_, __) => const ColoredBox(
|
||||
color: Colors.red,
|
||||
),
|
||||
refreshListenable: GoRouterRefreshStream(authenticationCubit.stream),
|
||||
redirect: (context, state) {
|
||||
final authState = authenticationCubit.state;
|
||||
|
||||
if (authState != previous) {
|
||||
previous = authState;
|
||||
// Check if current user is logged in
|
||||
final loggedIn =
|
||||
authState.status == AuthenticationStatus.authenticated;
|
||||
|
||||
// Checking if current path is onboarding or not
|
||||
final isOnboarding = AppRouter.publicRoutes.contains(state.subloc);
|
||||
|
||||
if (!loggedIn) {
|
||||
debugPrint('Not logged');
|
||||
if (isOnboarding) {
|
||||
return null;
|
||||
} else {
|
||||
return '/';
|
||||
}
|
||||
} else {
|
||||
debugPrint('Logged');
|
||||
if (isOnboarding) {
|
||||
return '/home';
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
);
|
||||
|
||||
return MultiRepositoryProvider(
|
||||
providers: [
|
||||
RepositoryProvider<AuthenticationRepository>.value(
|
||||
value: authenticationRepository,
|
||||
),
|
||||
],
|
||||
child: MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider<AuthenticationCubit<int>>.value(
|
||||
value: authenticationCubit,
|
||||
),
|
||||
BlocProvider<SignUpCubit<int>>(
|
||||
create: (_) => SignUpCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
),
|
||||
BlocProvider<SignInCubit<int>>(
|
||||
create: (_) => SignInCubit(
|
||||
authenticationRepository: authenticationRepository,
|
||||
),
|
||||
),
|
||||
],
|
||||
child: MaterialApp.router(
|
||||
title: 'Demo Authentication',
|
||||
debugShowCheckedModeBanner: false,
|
||||
routerConfig: router,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class GoRouterRefreshStream extends ChangeNotifier {
|
||||
GoRouterRefreshStream(Stream<dynamic> stream) {
|
||||
notifyListeners();
|
||||
_subscription = stream.asBroadcastStream().listen(
|
||||
(dynamic _) => notifyListeners(),
|
||||
);
|
||||
}
|
||||
|
||||
late final StreamSubscription<dynamic> _subscription;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_subscription.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: home_page.dart
|
||||
// Created Date: 19/08/2022 14:38:24
|
||||
// Last Modified: Wed Nov 09 2022
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:example_router/presentation/features/sub/sub_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';
|
||||
|
||||
class HomePage extends StatelessWidget {
|
||||
const HomePage({Key? key}) : super(key: key);
|
||||
|
||||
static String pageName = 'Home';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Home'),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: () => context.read<AuthenticationCubit<int>>().signOut(),
|
||||
icon: const Icon(Icons.logout_rounded))
|
||||
],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
AuthenticationBuilder<int>(
|
||||
authenticated: (context, accountWrapper) =>
|
||||
Text('Logged as ${accountWrapper.account?.email} | GeneratedId is ${accountWrapper.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')),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: sign_in_page.dart
|
||||
// Created Date: 19/08/2022 12:41:42
|
||||
// Last Modified: 19/08/2022 15:26:36
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:example_router/presentation/features/sign_in/widgets/sign_in_form.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class SignInPage extends StatelessWidget {
|
||||
const SignInPage({Key? key}) : super(key: key);
|
||||
|
||||
static String pageName = 'SignIn';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Sign In')),
|
||||
body: const Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
child: SingleChildScrollView(
|
||||
child: SignInForm(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: sign_in_form.dart
|
||||
// Created Date: 19/08/2022 15:24:37
|
||||
// Last Modified: Fri Nov 11 2022
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
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<SignInCubit<int>>(
|
||||
field: AuthFormField.email,
|
||||
builder: ((context, cubit, state, field, inputValid) {
|
||||
return TextField(
|
||||
onChanged: (email) => cubit.emailChanged(email),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Email',
|
||||
helperText: '',
|
||||
errorText: !inputValid ? 'Invalid email' : null,
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PasswordInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return InputBuilder<SignInCubit<int>>(
|
||||
field: AuthFormField.password,
|
||||
builder: ((context, cubit, state, field, inputValid) {
|
||||
return TextField(
|
||||
onChanged: (pwd) => cubit.passwordChanged(pwd),
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Password',
|
||||
helperText: '',
|
||||
errorText: !inputValid ? 'Invalid password' : null,
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SignInButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SubmitBuilder<SignInCubit<int>>(
|
||||
builder: ((context, cubit, status) {
|
||||
return status.isSubmissionInProgress
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: status.isValidated ? () => cubit.submit() : null,
|
||||
child: const Text('Sign in'),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SignInForm extends StatelessWidget {
|
||||
const SignInForm({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SignInListener<int>(
|
||||
onError: (context, status, errorMessage) => ScaffoldMessenger.of(context)
|
||||
..hideCurrentSnackBar()
|
||||
..showSnackBar(
|
||||
SnackBar(content: Text(errorMessage ?? 'Sign In Failure')),
|
||||
),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
_EmailInput(),
|
||||
const SizedBox(height: 8),
|
||||
_PasswordInput(),
|
||||
const SizedBox(height: 16),
|
||||
_SignInButton(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: sign_up_page.dart
|
||||
// Created Date: 19/08/2022 12:41:27
|
||||
// Last Modified: 19/08/2022 14:58:51
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:example_router/presentation/features/sign_up/widgets/sign_up_form.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class SignUpPage extends StatelessWidget {
|
||||
const SignUpPage({Key? key}) : super(key: key);
|
||||
|
||||
static String pageName = 'SignUp';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Sign Up')),
|
||||
body: const Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
child: SingleChildScrollView(
|
||||
child: SignUpForm(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: sign_up_form.dart
|
||||
// Created Date: 19/08/2022 14:41:08
|
||||
// Last Modified: Fri Nov 11 2022
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
import 'package:example_router/core/constants/form_field.dart';
|
||||
import 'package:flutter/material.dart' hide FormField;
|
||||
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<SignUpCubit<int>>(
|
||||
field: AuthFormField.email,
|
||||
builder: ((context, cubit, state, field, inputValid) {
|
||||
return TextField(
|
||||
onChanged: (email) => cubit.emailChanged(email),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Email',
|
||||
helperText: '',
|
||||
errorText: !inputValid ? 'Invalid email' : null,
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PasswordInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return InputBuilder<SignUpCubit<int>>(
|
||||
field: AuthFormField.password,
|
||||
builder: ((context, cubit, state, field, inputValid) {
|
||||
return TextField(
|
||||
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: !inputValid ? 'Invalid password' : null,
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ConfirmPasswordInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return InputBuilder<SignUpCubit<int>>(
|
||||
field: AppFormField.confirmedPassword,
|
||||
builder: ((context, cubit, state, field, inputValid) {
|
||||
return TextField(
|
||||
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: !inputValid ? 'Passwords do not match' : null,
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SignUpButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SubmitBuilder<SignUpCubit<int>>(
|
||||
builder: ((context, cubit, status) {
|
||||
return status.isSubmissionInProgress
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: status.isValidated ? () => cubit.submit() : null,
|
||||
child: const Text('Sign up'),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SignUpForm extends StatelessWidget {
|
||||
const SignUpForm({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SignUpListener<int>(
|
||||
onError: (context, status, errorMessage) =>
|
||||
ScaffoldMessenger.of(context)
|
||||
..hideCurrentSnackBar()
|
||||
..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(),
|
||||
],
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: sub_page.dart
|
||||
// Created Date: 19/08/2022 16:10:05
|
||||
// Last Modified: Wed Nov 09 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';
|
||||
|
||||
class SubPage extends StatelessWidget {
|
||||
const SubPage({Key? key}) : super(key: key);
|
||||
|
||||
static String pageName = 'Sub';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Sub'),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: () => context.read<AuthenticationCubit<int>>().signOut(),
|
||||
icon: const Icon(Icons.logout_rounded))
|
||||
],
|
||||
),
|
||||
body: const Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
child: SingleChildScrollView(
|
||||
child: Text('Another page'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
// Author: Hugo Pointcheval
|
||||
// Email: git@pcl.ovh
|
||||
// -----
|
||||
// File: welcome_page.dart
|
||||
// Created Date: 19/08/2022 12:33:21
|
||||
// Last Modified: Wed Nov 09 2022
|
||||
// -----
|
||||
// Copyright (c) 2022
|
||||
|
||||
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:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class WelcomePage extends StatelessWidget {
|
||||
const WelcomePage({Key? key}) : super(key: key);
|
||||
|
||||
static String pageName = 'Welcome';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Welcome'),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: () => context.pushNamed(SignUpPage.pageName),
|
||||
child: const Text('Sign Up')),
|
||||
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'))
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,39 +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:authentication_bloc_example/sign_up/widgets/sign_up_form.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class SignUpPage extends StatelessWidget {
|
||||
const SignUpPage({Key? key}) : super(key: key);
|
||||
|
||||
static Route route() {
|
||||
return MaterialPageRoute<void>(builder: (_) => const SignUpPage());
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Sign Up')),
|
||||
body: const Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
child: SingleChildScrollView(
|
||||
child: SignUpForm(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,325 +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 'dart:developer';
|
||||
|
||||
import 'package:authentication_bloc_example/app/app.dart';
|
||||
import 'package:authentication_bloc_example/constants.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 _NameInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (name) => context
|
||||
.read<SignUpCubit>()
|
||||
.dataChanged(formFieldName, Name.dirty(name)),
|
||||
keyboardType: TextInputType.name,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'name',
|
||||
helperText: '',
|
||||
errorText:
|
||||
state.data.isNotValid(formFieldName) ? 'invalid name' : null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PhoneInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (phone) => context
|
||||
.read<SignUpCubit>()
|
||||
.dataChanged(formFieldPhone, Phone.dirty(phone)),
|
||||
keyboardType: TextInputType.phone,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'phone',
|
||||
helperText: '',
|
||||
errorText: state.data.isNotValid(formFieldPhone)
|
||||
? 'invalid phone'
|
||||
: null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SirenInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (siren) => context
|
||||
.read<SignUpCubit>()
|
||||
.dataChanged(formFieldSiren, Siren.dirty(siren)),
|
||||
keyboardType: TextInputType.number,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'siren',
|
||||
helperText: '',
|
||||
errorText: state.data.isNotValid(formFieldSiren)
|
||||
? 'invalid SIREN'
|
||||
: null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _IbanInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (iban) => context
|
||||
.read<SignUpCubit>()
|
||||
.dataChanged(formFieldIban, Iban.dirty(iban)),
|
||||
keyboardType: TextInputType.text,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'iban',
|
||||
helperText: '',
|
||||
errorText:
|
||||
state.data.isNotValid(formFieldIban) ? 'invalid IBAN' : null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _EmailInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
buildWhen: (previous, current) => previous.email != current.email,
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (email) => context.read<SignUpCubit>().emailChanged(email),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'email',
|
||||
helperText: '',
|
||||
errorText: state.email.invalid ? 'invalid email' : null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PasswordInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (password) {
|
||||
context.read<SignUpCubit>().passwordChanged(password);
|
||||
context.read<SignUpCubit>().dataChanged(
|
||||
formFieldConfirmedPassword,
|
||||
ConfirmedPassword.dirty(
|
||||
password: password,
|
||||
value: context
|
||||
.read<SignUpCubit>()
|
||||
.state
|
||||
.data
|
||||
.valueOf<String>(formFieldConfirmedPassword),
|
||||
),
|
||||
);
|
||||
},
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'password',
|
||||
helperText: '',
|
||||
errorText: state.password.invalid ? 'invalid password' : null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ConfirmPasswordInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
return TextField(
|
||||
onChanged: (confirmPassword) => context
|
||||
.read<SignUpCubit>()
|
||||
.dataChanged(
|
||||
formFieldConfirmedPassword,
|
||||
ConfirmedPassword.dirty(
|
||||
password: context.read<SignUpCubit>().state.password.value,
|
||||
value: confirmPassword,
|
||||
),
|
||||
),
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'confirm password',
|
||||
helperText: '',
|
||||
errorText: state.data.isNotValid(formFieldConfirmedPassword)
|
||||
? 'passwords do not match'
|
||||
: null,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _CheckIsProInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListTile(
|
||||
title: const Text('Are you a pro?'),
|
||||
trailing: BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
return Checkbox(
|
||||
value: state.data.valueOf<bool>(formFieldPro),
|
||||
onChanged: (isPro) {
|
||||
final value =
|
||||
isPro!; // tristate is false, so value can't be null
|
||||
|
||||
context.read<SignUpCubit>().dataChanged(
|
||||
formFieldPro,
|
||||
Boolean.dirty(value: value),
|
||||
);
|
||||
|
||||
if (value) {
|
||||
context.read<SignUpCubit>().updateFormData(
|
||||
App.getProFormData(),
|
||||
operation: SetOperation.union);
|
||||
} else {
|
||||
context.read<SignUpCubit>().updateFormData(
|
||||
App.getNormalFormData(),
|
||||
operation: SetOperation.intersection);
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SignUpButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
buildWhen: (previous, current) => previous.status != current.status,
|
||||
builder: (context, state) {
|
||||
return state.status.isSubmissionInProgress
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: state.status.isValidated
|
||||
? () => context.read<SignUpCubit>().signUpFormSubmitted()
|
||||
: null,
|
||||
child: const Text('SIGN UP'),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _DebugButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
return ElevatedButton(
|
||||
onPressed: () {
|
||||
// log(state.toString());
|
||||
log(state.data.toMap().toString());
|
||||
},
|
||||
child: const Text('DEBUG'),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SignUpForm extends StatelessWidget {
|
||||
const SignUpForm({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocListener<SignUpCubit, SignUpState>(
|
||||
listener: (context, state) {
|
||||
if (state.status.isSubmissionSuccess) {
|
||||
Navigator.of(context).pop();
|
||||
} else if (state.status.isSubmissionFailure) {
|
||||
ScaffoldMessenger.of(context)
|
||||
..hideCurrentSnackBar()
|
||||
..showSnackBar(
|
||||
SnackBar(content: Text(state.errorMessage ?? 'Sign Up Failure')),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: SizedBox(
|
||||
height: MediaQuery.of(context).size.height,
|
||||
child: Align(
|
||||
alignment: const Alignment(0, -1 / 3),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_NameInput(),
|
||||
const SizedBox(height: 8),
|
||||
_PhoneInput(),
|
||||
const SizedBox(height: 8),
|
||||
_CheckIsProInput(),
|
||||
const SizedBox(height: 8),
|
||||
BlocBuilder<SignUpCubit, SignUpState>(
|
||||
builder: (context, state) {
|
||||
if (state.data.valueOf<bool>(formFieldPro)) {
|
||||
return Column(children: [
|
||||
_SirenInput(),
|
||||
const SizedBox(height: 8),
|
||||
_IbanInput(),
|
||||
const SizedBox(height: 8),
|
||||
]);
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
_EmailInput(),
|
||||
const SizedBox(height: 8),
|
||||
_PasswordInput(),
|
||||
const SizedBox(height: 8),
|
||||
_ConfirmPasswordInput(),
|
||||
const SizedBox(height: 8),
|
||||
_SignUpButton(),
|
||||
const SizedBox(height: 8),
|
||||
_DebugButton(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user