doc(auth): update example

This commit is contained in:
2022-11-09 01:48:32 -05:00
parent 0d00a67b7c
commit 79fdd3c837
51 changed files with 1327 additions and 61 deletions
@@ -3,23 +3,24 @@
// -----
// File: bootstrap.dart
// Created Date: 19/08/2022 15:05:17
// Last Modified: 19/08/2022 15:21:47
// Last Modified: Wed Nov 09 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:example_router/firebase_options.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
Future<void> bootstrap(FutureOr<Widget> Function() builder) async {
await runZonedGuarded(
() async {
WidgetsFlutterBinding.ensureInitialized();
Bloc.observer = AppBlocObserver();
FlutterError.onError = (details) {
debugPrint(details.toString());
@@ -29,9 +30,7 @@ Future<void> bootstrap(FutureOr<Widget> Function() builder) async {
options: DefaultFirebaseOptions.currentPlatform,
);
GoRouter.setUrlPathStrategy(UrlPathStrategy.path);
Bloc.observer = AppBlocObserver();
await GetItInitializer.init();
runApp(await builder());
},
@@ -0,0 +1,34 @@
// 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';
final getIt = GetIt.I;
abstract class GetItInitializer {
static Future<void> init() async {
getIt
..registerLazySingleton<AuthenticationRemoteDataSource>(
() => AuthenticationFirebaseDataSourceImpl(),
)
..registerLazySingleton<AuthenticationLocalDataSource<int>>(
() => AuthenticationCacheDataSourceImpl<int>(),
);
await getIt.allReady();
}
}
@@ -24,12 +24,12 @@ class DefaultFirebaseOptions {
}
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return android;
case TargetPlatform.iOS:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for ios - '
'DefaultFirebaseOptions have not been configured for android - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
case TargetPlatform.iOS:
return ios;
case TargetPlatform.macOS:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for macos - '
@@ -52,12 +52,15 @@ class DefaultFirebaseOptions {
}
}
static const FirebaseOptions android = FirebaseOptions(
apiKey: 'AIzaSyAYS14uXupkS158Q5QAFP1864UrUN_yDSk',
appId: '1:136771801992:android:ac3cfeb99fb0763e97203d',
static const FirebaseOptions ios = FirebaseOptions(
apiKey: 'AIzaSyCDbbhjbFrQwLXuIANdJzjkDk8uOETnn7w',
appId: '1:136771801992:ios:bcdca68d2b7d227097203d',
messagingSenderId: '136771801992',
projectId: 'tchat-beta',
databaseURL: 'https://tchat-beta.firebaseio.com',
storageBucket: 'tchat-beta.appspot.com',
androidClientId: '136771801992-n2pq8oqutvrqj58e05hbavvc7n1jdfjb.apps.googleusercontent.com',
iosClientId: '136771801992-p629tpo9bk3hcm2955s5ahivdla57ln9.apps.googleusercontent.com',
iosBundleId: 'com.example.exampleRouter',
);
}
@@ -3,10 +3,13 @@
// -----
// File: app.dart
// Created Date: 19/08/2022 12:05:38
// Last Modified: Fri Aug 26 2022
// Last Modified: Wed Nov 09 2022
// -----
// Copyright (c) 2022
import 'dart:async';
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';
@@ -15,10 +18,18 @@ 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_type_utils/wyatt_type_utils.dart';
class App extends StatelessWidget {
final AuthenticationRepository authenticationRepository =
AuthenticationRepositoryFirebase();
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);
});
App({Key? key}) : super(key: key);
@@ -26,13 +37,8 @@ class App extends StatelessWidget {
Widget build(BuildContext context) {
AuthenticationState? previous;
final AuthenticationCubit authenticationCubit = AuthenticationCubit(
authenticationRepository: authenticationRepository,
onAuthSuccess: (user) async {
debugPrint(user.toString());
return {};
},
);
final AuthenticationCubit<int> authenticationCubit =
AuthenticationCubit(authenticationRepository: authenticationRepository);
final GoRouter router = GoRouter(
initialLocation: '/',
@@ -42,7 +48,7 @@ class App extends StatelessWidget {
color: Colors.red,
),
refreshListenable: GoRouterRefreshStream(authenticationCubit.stream),
redirect: (state) {
redirect: (context, state) {
final authState = authenticationCubit.state;
if (authState != previous) {
@@ -62,8 +68,7 @@ class App extends StatelessWidget {
return state.namedLocation(WelcomePage.pageName);
}
} else {
final email = authState.user?.email;
debugPrint('Logged as: $email');
debugPrint('Logged');
if (isOnboarding) {
return state.namedLocation(HomePage.pageName);
} else {
@@ -83,17 +88,13 @@ class App extends StatelessWidget {
],
child: MultiBlocProvider(
providers: [
BlocProvider<AuthenticationCubit>.value(
value: authenticationCubit..init(),
BlocProvider<AuthenticationCubit<int>>.value(
value: authenticationCubit,
),
BlocProvider<SignUpCubit>(
create: (_) => SignUpCubit(
authenticationRepository: authenticationRepository,
formData: Forms.getNormalData(),
onSignUpSuccess: (state, uid) async {
debugPrint(state.toString());
debugPrint(uid);
},
),
),
BlocProvider<SignInCubit>(
@@ -113,3 +114,20 @@ class App extends StatelessWidget {
);
}
}
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();
}
}
@@ -3,7 +3,7 @@
// -----
// File: home_page.dart
// Created Date: 19/08/2022 14:38:24
// Last Modified: 19/08/2022 16:12:22
// Last Modified: Wed Nov 09 2022
// -----
// Copyright (c) 2022
@@ -25,7 +25,7 @@ class HomePage extends StatelessWidget {
title: const Text('Home'),
actions: [
IconButton(
onPressed: () => context.read<AuthenticationCubit>().logOut(),
onPressed: () => context.read<AuthenticationCubit<int>>().signOut(),
icon: const Icon(Icons.logout_rounded))
],
),
@@ -34,11 +34,12 @@ class HomePage extends StatelessWidget {
child: SingleChildScrollView(
child: Column(
children: [
BlocBuilder<AuthenticationCubit, AuthenticationState>(
builder: (context, state) {
final email = state.user?.email;
return Text('Logged as $email');
},
AuthenticationBuilder<int>(
authenticated: (context, accountWrapper) =>
Text('Logged as ${accountWrapper.account?.email}'),
unauthenticated: (context) =>
const Text('Not logged (unauthenticated)'),
unknown: (context) => const Text('Not logged (unknown)'),
),
const SizedBox(
height: 8,
@@ -3,7 +3,7 @@
// -----
// File: sign_in_form.dart
// Created Date: 19/08/2022 15:24:37
// Last Modified: 19/08/2022 16:35:01
// Last Modified: Wed Nov 09 2022
// -----
// Copyright (c) 2022
@@ -79,9 +79,7 @@ class SignInForm extends StatelessWidget {
Widget build(BuildContext context) {
return BlocListener<SignInCubit, SignInState>(
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(
@@ -3,7 +3,7 @@
// -----
// File: sub_page.dart
// Created Date: 19/08/2022 16:10:05
// Last Modified: 19/08/2022 16:10:44
// Last Modified: Wed Nov 09 2022
// -----
// Copyright (c) 2022
@@ -23,7 +23,7 @@ class SubPage extends StatelessWidget {
title: const Text('Sub'),
actions: [
IconButton(
onPressed: () => context.read<AuthenticationCubit>().logOut(),
onPressed: () => context.read<AuthenticationCubit<int>>().signOut(),
icon: const Icon(Icons.logout_rounded))
],
),
@@ -3,16 +3,14 @@
// -----
// File: welcome_page.dart
// Created Date: 19/08/2022 12:33:21
// Last Modified: 19/08/2022 15:56:05
// 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:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
class WelcomePage extends StatelessWidget {
const WelcomePage({Key? key}) : super(key: key);
@@ -24,12 +22,6 @@ class WelcomePage extends StatelessWidget {
return Scaffold(
appBar: AppBar(
title: const Text('Welcome'),
actions: [
IconButton(
onPressed: () => context.read<AuthenticationCubit>().changeStatus(
context.read<AuthenticationCubit>().state.user!),
icon: const Icon(Icons.refresh_rounded))
],
),
body: SingleChildScrollView(
child: Column(