Skip to content

Commit

Permalink
Add basic login and register page
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasatisocco committed Jul 20, 2023
1 parent 054e4ab commit 1240729
Show file tree
Hide file tree
Showing 17 changed files with 642 additions and 38 deletions.
8 changes: 4 additions & 4 deletions lib/admin_panel/admin_login/cubit/admin_login_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import 'package:firestore_repository/firestore_repository.dart';
part 'admin_login_state.dart';

class AdminLoginCubit extends Cubit<AdminLoginState> {
AdminLoginCubit(
{required AuthRepository authRepository,
required FirestoreRepository firestoreRepository})
: _authRepository = authRepository,
AdminLoginCubit({
required AuthRepository authRepository,
required FirestoreRepository firestoreRepository,
}) : _authRepository = authRepository,
_firestoreRepository = firestoreRepository,
super(AdminLoginInitial());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AdminTestsCubit extends Cubit<AdminTestsState> {
Future<void> getTest(UserDataModel user) async {
emit(AdminTestsFetching());
try {
final test = await _firestoreRepository.getTest(user.id!);
final test = await _firestoreRepository.getTest(user.authId!);
emit(AdminTestsFetched(test: CoachingTest.fromMap(test!), user: user));
} catch (_) {
emit(AdminTestsError());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AdminUsersCubit extends Cubit<AdminUsersState> {
_firestoreRepository.listenUserList().listen((snapshots) {
final users = snapshots.docs
.map(
(e) => UserDataModel.fromMap(e.data()).copyWith(id: e.id),
(e) => UserDataModel.fromMap(e.data()).copyWith(authId: e.id),
)
.toList();
emit(AdminUsersFetched(users: users));
Expand Down
22 changes: 12 additions & 10 deletions lib/admin_panel/admin_page/view/admin_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ class _AdminViewState extends State<AdminView> {
}
final users = state.users
..sort(
(a, b) => b.createdAt.compareTo(a.createdAt),
(a, b) => (b.createdAt ?? DateTime.now()).compareTo(
a.createdAt ?? DateTime.now(),
),
);
return ListView.builder(
itemCount: users.length,
itemBuilder: (context, index) {
final testId = users[index].id;
final testId = users[index].authId;
return ListTile(
title: Text(users[index].name),
subtitle: Text(users[index].email),
title: Text(users[index].name ?? ''),
subtitle: Text(users[index].email ?? ''),
selected: selectedId == testId,
onTap: () async {
if (testId == null) return;
Expand Down Expand Up @@ -233,7 +235,7 @@ class _AdminViewState extends State<AdminView> {
fontWeight: FontWeight.bold,
),
),
subtitle: Text(state.user.name),
subtitle: Text(state.user.name ?? ''),
),
ListTile(
title: Text(
Expand All @@ -242,7 +244,7 @@ class _AdminViewState extends State<AdminView> {
fontWeight: FontWeight.bold,
),
),
subtitle: Text(state.user.email),
subtitle: Text(state.user.email ?? ''),
),
ListTile(
title: Text(
Expand All @@ -251,7 +253,7 @@ class _AdminViewState extends State<AdminView> {
fontWeight: FontWeight.bold,
),
),
subtitle: Text(state.user.nationality),
subtitle: Text(state.user.nationality ?? ''),
),
ListTile(
title: Text(
Expand All @@ -260,7 +262,7 @@ class _AdminViewState extends State<AdminView> {
fontWeight: FontWeight.bold,
),
),
subtitle: Text(state.user.residence),
subtitle: Text(state.user.residence ?? ''),
),
ListTile(
title: Text(
Expand All @@ -269,7 +271,7 @@ class _AdminViewState extends State<AdminView> {
fontWeight: FontWeight.bold,
),
),
subtitle: Text(state.user.birthDate),
subtitle: Text(state.user.birthDate ?? ''),
),
ListTile(
title: Text(
Expand All @@ -278,7 +280,7 @@ class _AdminViewState extends State<AdminView> {
fontWeight: FontWeight.bold,
),
),
subtitle: Text(state.user.certificateDate),
subtitle: Text(state.user.certificateDate ?? ''),
),
...state.test.questions.map((e) {
return ListTile(
Expand Down
39 changes: 28 additions & 11 deletions lib/app/view/app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:auth_repository/auth_repository.dart';
import 'package:coaching/admin_panel/admin_login/view/adming_login_page.dart';
import 'package:coaching/app/cubit/localizations_cubit.dart';
import 'package:coaching/authentication/login/view/login_page.dart';
import 'package:coaching/authentication/register/view/register_page.dart';
import 'package:coaching/coaching_test/models/test_model.dart';
import 'package:coaching/coaching_test/view/coaching_test_page.dart';
import 'package:coaching/l10n/l10n.dart';
Expand Down Expand Up @@ -96,8 +98,22 @@ class _AppViewState extends State<AppView> {

GoRouter router(BuildContext context) {
return GoRouter(
initialLocation: '/welcome',
initialLocation: '/login',
routes: <GoRoute>[
GoRoute(
path: '/login',
name: LoginPage.name,
builder: (_, state) {
return const LoginPage();
},
),
GoRoute(
path: '/register',
name: RegisterPage.name,
builder: (_, state) {
return const RegisterPage();
},
),
GoRoute(
path: '/welcome',
name: WelcomePage.name,
Expand Down Expand Up @@ -140,16 +156,17 @@ class _AppViewState extends State<AppView> {
),
],
redirect: (context, state) async {
final location = state.location;
if (location == '/admin_login') return location;
try {
final userId = context.read<DataPersistenceRepository>().getUserId();
if (userId == null) throw Exception('User id is null.');
} catch (_) {
await context.read<DataPersistenceRepository>().deleteCoachingTest();
return '/welcome';
}
return null;
return state.location;
// final location = state.location;
// if (location == '/admin_login') return location;
// try {
// final userId = context.read<DataPersistenceRepository>().getUserId();
// if (userId == null) throw Exception('User id is null.');
// } catch (_) {
// await context.read<DataPersistenceRepository>().deleteCoachingTest();
// return '/welcome';
// }
// return null;
},
);
}
Expand Down
52 changes: 52 additions & 0 deletions lib/authentication/login/cubit/login_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:auth_repository/auth_repository.dart';
import 'package:bloc/bloc.dart';
import 'package:coaching/welcome/models/user_date_model.dart';
import 'package:firestore_repository/firestore_repository.dart';

part 'login_state.dart';

class LoginCubit extends Cubit<LoginState> {
LoginCubit({
required AuthRepository authRepository,
required FirestoreRepository firestoreRepository,
}) : _authRepository = authRepository,
_firestoreRepository = firestoreRepository,
super(const LoginInitial());

final AuthRepository _authRepository;
final FirestoreRepository _firestoreRepository;

/// Signs in with the given [email] and [password].
Future<void> logInWithEmailAndPassword(
String email,
String password,
) async {
emit(const LoginLoading());
try {
final userCredentials = await _authRepository.signInWithEmailAndPassword(
email,
password,
);
final userData = await _firestoreRepository.getUserByAuthId(
userCredentials.user!.uid,
);

if (userData == null) {
final user = UserDataModel.newUser(
authId: userCredentials.user!.uid,
createdAt: DateTime.now(),
email: email,
name: userCredentials.user?.displayName,
);
await _firestoreRepository.addUser(user.toMap());
return emit(LoginSuccess(user));
}

final user = UserDataModel.fromMap(userData);
return emit(LoginSuccess(user));
} catch (_) {
await _authRepository.signOut();
emit(const LoginFailure());
}
}
}
27 changes: 27 additions & 0 deletions lib/authentication/login/cubit/login_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
part of 'login_cubit.dart';

abstract class LoginState {
const LoginState();
}

/// The initial state of the [LoginCubit].
class LoginInitial extends LoginState {
const LoginInitial() : super();
}

/// The state of the [LoginCubit] when a login attempt is in progress.
class LoginLoading extends LoginState {
const LoginLoading() : super();
}

/// The state of the [LoginCubit] when a login attempt has succeeded.
class LoginSuccess extends LoginState {
const LoginSuccess(this.userDataModel) : super();

final UserDataModel userDataModel;
}

/// The state of the [LoginCubit] when a login attempt has failed.
class LoginFailure extends LoginState {
const LoginFailure() : super();
}
Loading

0 comments on commit 1240729

Please sign in to comment.