-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
054e4ab
commit 1240729
Showing
17 changed files
with
642 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.