Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
refactor(auth): cleanup home controller
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammedKpln committed Feb 24, 2023
1 parent 000e85e commit 5e15c90
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
25 changes: 25 additions & 0 deletions lib/core/auth/controllers/auth.controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:mobx/mobx.dart';
import 'package:spamify/core/storage/account.storage.dart';
import 'package:spamify/core/storage/isar/local_account.db.dart';
import 'package:spamify/core/storage/messages.storage.dart';
import 'package:spamify/types/account.dart';
part 'auth.controller.g.dart';

enum AuthState { loggedIn, none }
Expand Down Expand Up @@ -42,4 +43,28 @@ abstract class _AuthControllerBase with Store {
account.value = null;
authState.value = AuthState.none;
}

@action
Future<void> login(
{required AccountModel acc,
required String password,
required String token}) async {
await _cleanUpIfAccountAlreadyExists();

account.value = LocalAccount(
address: acc.address,
password: password,
token: token,
accountId: acc.id,
);

await _accountStorage.saveAccount(account.value!);
authState.value = AuthState.loggedIn;
}

Future<void> _cleanUpIfAccountAlreadyExists() async {
if (isLoggedIn) {
await logout();
}
}
}
12 changes: 3 additions & 9 deletions lib/features/home/controllers/home.controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:injectable/injectable.dart';
import 'package:mobx/mobx.dart';
import 'package:spamify/core/auth/controllers/auth.controller.dart';
import 'package:spamify/core/storage/app.storage.dart';
import 'package:spamify/core/storage/isar/local_account.db.dart';
import 'package:spamify/features/home/repositories/account.repository.dart';
import 'package:spamify/core/services/dio.service.dart';
import 'package:spamify/core/storage/account.storage.dart';
Expand Down Expand Up @@ -66,16 +65,11 @@ abstract class _HomeViewControllerBase with Store {
domains.hydraMember[0].domain, password);

final token = await _accountRepository.login(_account.address, password);
_authController.account.value = LocalAccount(
address: _account.address,
password: password,
token: token.token,
accountId: _account.id);

await _accountStorage.saveAccount(_authController.account.value!);
_authController.authState.value = AuthState.loggedIn;
await _authController.login(
acc: _account, password: password, token: token.token);

textFieldController.text = _authController.account.value?.address ?? "";
textFieldController.text = _account.address;

isLoading = false;
}
Expand Down

0 comments on commit 5e15c90

Please sign in to comment.