153 lines
4.9 KiB
Dart
153 lines
4.9 KiB
Dart
// 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: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<Map<String, dynamic>> onAuthSuccess(UserInterface user) async {
|
|
if (user.isNotEmpty && !user.isAnonymous) {
|
|
// Check if user is register in Firesore.
|
|
DocumentSnapshot firestoreUser = await FirebaseFirestore.instance
|
|
.collection('users')
|
|
.doc(user.uid)
|
|
.get();
|
|
return {
|
|
'user':
|
|
UserFirestore.fromMap(firestoreUser.data() as Map<String, dynamic>),
|
|
...firestoreUser.data() as Map<String, dynamic>? ?? {}
|
|
};
|
|
} else {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
// On Sign Up Success.
|
|
Future<void> onSignUpSuccess(SignUpState state, String? uid) async {
|
|
if (uid != null) {
|
|
final data = state.data.toMap();
|
|
final user = {'uid': uid, 'email': state.email.value, ...data};
|
|
await FirebaseFirestore.instance.collection('users').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<AuthenticationRepositoryInterface>(
|
|
create: (context) => _authenticationRepository,
|
|
),
|
|
],
|
|
child: MultiBlocProvider(
|
|
providers: [
|
|
BlocProvider<AuthenticationCubit>(
|
|
create: (context) => _authenticationCubit..init(),
|
|
),
|
|
BlocProvider<SignUpCubit>(
|
|
create: (context) => _signUpCubit,
|
|
),
|
|
BlocProvider<SignInCubit>(
|
|
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(),
|
|
),
|
|
);
|
|
}
|
|
}
|