You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2KB

  1. import 'dart:async';
  2. import '../../utils/const_enum.dart';
  3. import '../../utils/local_storage.dart';
  4. class AuthenticationRepository {
  5. final _controller = StreamController<AuthenticationStatus>();
  6. Stream<AuthenticationStatus> get status async* {
  7. var status = await checkAuthStatus();
  8. switch (status) {
  9. case AuthenticationStatus.authenticated:
  10. yield AuthenticationStatus.authenticated;
  11. break;
  12. case AuthenticationStatus.unauthenticated:
  13. yield AuthenticationStatus.unauthenticated;
  14. break;
  15. default:
  16. yield AuthenticationStatus.unauthenticated;
  17. break;
  18. }
  19. yield* _controller.stream;
  20. }
  21. Future<AuthenticationStatus> checkAuthStatus() async {
  22. try {
  23. var isLogged = LocalStorage.getBool(LocalStorageKey.is_logged);
  24. if (isLogged) {
  25. return AuthenticationStatus.authenticated;
  26. } else {
  27. return AuthenticationStatus.unauthenticated;
  28. }
  29. } catch (_) {
  30. return AuthenticationStatus.unauthenticated;
  31. }
  32. }
  33. void logOut() {
  34. LocalStorage.clearUserInfo();
  35. _controller.add(AuthenticationStatus.unauthenticated);
  36. }
  37. void dispose() => _controller.close();
  38. }