rsflutterapp

Bahnhofsfoto-App for Android and iOS using Flutter
git clone git://archive.git.mtrnord.blog/MTRNord/rsflutterapp.git
Log | Files | Refs | README

AuthenticationBloc.dart (1170B)


      1 import 'package:bloc/bloc.dart';
      2 import 'package:flutter/cupertino.dart';
      3 
      4 import '../repos/UserRepository.dart';
      5 import '../states/AuthenticationState.dart';
      6 import '../events/AuthenticationEvent.dart';
      7 
      8 class AuthenticationBloc
      9     extends Bloc<AuthenticationEvent, AuthenticationState> {
     10   final UserRepository userRepository;
     11 
     12   AuthenticationBloc({@required this.userRepository})
     13       : assert(userRepository != null);
     14 
     15   @override
     16   AuthenticationState get initialState => AuthenticationUninitialized();
     17 
     18   @override
     19   Stream<AuthenticationState> mapEventToState(
     20     AuthenticationEvent event,
     21   ) async* {
     22     if (event is AppStarted) {
     23       final bool hasToken = await userRepository.hasToken();
     24 
     25       if (hasToken) {
     26         yield AuthenticationAuthenticated();
     27       } else {
     28         yield AuthenticationNeeded();
     29       }
     30     }
     31 
     32     if (event is LoggedIn) {
     33       yield AuthenticationLoading();
     34       await userRepository.persistToken(event.token);
     35       yield AuthenticationAuthenticated();
     36     }
     37 
     38     if (event is LoggedOut) {
     39       yield AuthenticationLoading();
     40       await userRepository.deleteToken();
     41       yield AuthenticationNeeded();
     42     }
     43   }
     44 }