doc(auth): update example
This commit is contained in:
+37
-19
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
+8
-7
@@ -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,
|
||||
|
||||
+2
-4
@@ -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(
|
||||
|
||||
+2
-2
@@ -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))
|
||||
],
|
||||
),
|
||||
|
||||
+1
-9
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user