-
-
Save red-star25/c62fcaf5295ddb50364ee049a3489641 to your computer and use it in GitHub Desktop.
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
import 'package:bloc/bloc.dart'; | |
import 'package:bloc_auth/data/repositories/auth_repository.dart'; | |
import 'package:equatable/equatable.dart'; | |
import 'package:meta/meta.dart'; | |
part 'auth_event.dart'; | |
part 'auth_state.dart'; | |
class AuthBloc extends Bloc<AuthEvent, AuthState> { | |
final AuthRepository authRepository; | |
AuthBloc({required this.authRepository}) : super(UnAuthenticated()) { | |
// When User Presses the SignIn Button, we will send the SignInRequested Event to the AuthBloc to handle it and emit the Authenticated State if the user is authenticated | |
on<SignInRequested>((event, emit) async { | |
emit(Loading()); | |
try { | |
await authRepository.signIn( | |
email: event.email, password: event.password); | |
emit(Authenticated()); | |
} catch (e) { | |
emit(AuthError(e.toString())); | |
emit(UnAuthenticated()); | |
} | |
}); | |
// When User Presses the SignUp Button, we will send the SignUpRequest Event to the AuthBloc to handle it and emit the Authenticated State if the user is authenticated | |
on<SignUpRequested>((event, emit) async { | |
emit(Loading()); | |
try { | |
await authRepository.signUp( | |
email: event.email, password: event.password); | |
emit(Authenticated()); | |
} catch (e) { | |
emit(AuthError(e.toString())); | |
emit(UnAuthenticated()); | |
} | |
}); | |
// When User Presses the Google Login Button, we will send the GoogleSignInRequest Event to the AuthBloc to handle it and emit the Authenticated State if the user is authenticated | |
on<GoogleSignInRequested>((event, emit) async { | |
emit(Loading()); | |
try { | |
await authRepository.signInWithGoogle(); | |
emit(Authenticated()); | |
} catch (e) { | |
emit(AuthError(e.toString())); | |
emit(UnAuthenticated()); | |
} | |
}); | |
// When User Presses the SignOut Button, we will send the SignOutRequested Event to the AuthBloc to handle it and emit the UnAuthenticated State | |
on<SignOutRequested>((event, emit) async { | |
emit(Loading()); | |
await authRepository.signOut(); | |
emit(UnAuthenticated()); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment