// 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 . 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([ FormEntry(formFieldName, Name.pure()), FormEntry(formFieldPhone, Phone.pure()), FormEntry(formFieldPro, Boolean.pure()), FormEntry(formFieldConfirmedPassword, ConfirmedPassword.pure(), export: false), ]); } static FormData getProFormData() { return const FormData([ FormEntry(formFieldName, Name.pure()), FormEntry(formFieldPhone, Phone.pure()), FormEntry(formFieldPro, Boolean.pure()), FormEntry(formFieldSiren, Siren.pure()), FormEntry(formFieldIban, Iban.pure()), FormEntry(formFieldConfirmedPassword, ConfirmedPassword.pure(), export: false), ]); } // On Authentication Success. (Authenticated or Anonymous) // User callback. Future> 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), ...firestoreUser.data() as Map? ?? {} }; } } else { return {}; } } // On Sign Up Success. Future 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( create: (context) => _authenticationRepository, ), ], child: MultiBlocProvider( providers: [ BlocProvider( create: (context) => _authenticationCubit..init(), ), BlocProvider( create: (context) => _signUpCubit, ), BlocProvider( 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(), ), ); } }