chore(auth): remove old example and rename actual
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user