chore(auth): remove old example and rename actual

This commit is contained in:
2022-11-15 18:52:10 -05:00
parent 6eb40de72c
commit 4fd2a2a16c
110 changed files with 70 additions and 1830 deletions
@@ -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();
}
}
@@ -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')),
],
),
),
),
);
}
}
@@ -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(),
),
),
);
}
}
@@ -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(),
],
),
),
);
}
}
@@ -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(),
),
),
);
}
}
@@ -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(),
],
),
));
}
}
@@ -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'),
),
),
);
}
}
@@ -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'))
],
),
),
);
}
}