Commit 67b3d961 authored by Leon Tappe's avatar Leon Tappe 🔥
Browse files

store username in authbloc

parent 5de65327
......@@ -14,13 +14,15 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
final http.Client client;
String _token;
String _username;
AuthBloc(this.client) : super(AuthState.unauthorized()) {
_log.fine('$this started');
on<Login>((Login event, Emitter<AuthState> emit) async {
try {
await _postLogin(event.username, event.password);
_username = event.username;
await _postLogin(_username, event.password);
if (_token != null) emit(AuthState.authorized(_token));
} on NetworkException catch (e) {
emit(AuthState.exception(e));
......@@ -29,15 +31,19 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
on<Logout>((Logout event, Emitter<AuthState> emit) async {
_token = null;
_username = null;
emit(AuthState.unauthorized());
});
on<TokenLogin>((TokenLogin event, Emitter<AuthState> emit) async {
_token = event.token;
_username = event.username;
emit(AuthState.authorized(_token));
});
}
String get username => _username;
@override
Future<void> close() {
_log.fine('closing $this');
......@@ -54,7 +60,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
super.onTransition(transition);
}
void tokenLogin(String token) => add(TokenLogin(token));
void tokenLogin(String token, String username) => add(TokenLogin(token, username));
Future<void> _postLogin(String user, String password) async {
_log.finer('_postLogin: $user, $password');
......
......@@ -19,10 +19,11 @@ class Logout extends AuthEvent {}
class TokenLogin extends AuthEvent {
final String token;
final String username;
TokenLogin(this.token);
TokenLogin(this.token, this.username);
Map<String, dynamic> get toMap => {'token': token};
Map<String, dynamic> get toMap => {'token': token, 'username': username};
@override
String toString() => '[TokenLogin $toMap]';
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment