Skip to content

Instantly share code, notes, and snippets.

@red-star25
Last active January 8, 2022 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save red-star25/c62fcaf5295ddb50364ee049a3489641 to your computer and use it in GitHub Desktop.
Save red-star25/c62fcaf5295ddb50364ee049a3489641 to your computer and use it in GitHub Desktop.
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