-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathauth_bloc.dart
49 lines (44 loc) · 1.33 KB
/
auth_bloc.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import 'package:airqo/src/app/auth/models/input_model.dart';
import 'package:airqo/src/app/auth/repository/auth_repository.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
part 'auth_event.dart';
part 'auth_state.dart';
class AuthBloc extends Bloc<AuthEvent, AuthState> {
final AuthRepository authRepository;
AuthBloc(this.authRepository) : super(AuthInitial()) {
on<AuthEvent>((event, emit) async {
if (event is LoginUser) {
try {
emit(AuthLoading());
await authRepository.loginWithEmailAndPassword(
event.username, event.password);
emit(AuthLoaded(AuthPurpose.LOGIN));
} catch (e) {
debugPrint(e.toString());
emit(
AuthLoadingError(
e.toString(),
),
);
}
} else if (event is RegisterUser) {
try {
emit(AuthLoading());
await authRepository.registerWithEmailAndPassword(event.model);
emit(AuthLoaded(AuthPurpose.REGISTER));
} catch (e) {
debugPrint(e.toString());
emit(
AuthLoadingError(
e.toString(),
),
);
}
} else if (event is UseAsGuest) {
emit(GuestUser());
}
});
}
}