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

Commit

Permalink
fix(auth): auth state did not reflect ui elements
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammedKpln committed Feb 23, 2023
1 parent b990644 commit baa60a1
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 54 deletions.
17 changes: 10 additions & 7 deletions lib/core/auth/controllers/auth.controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,33 @@ enum AuthState { loggedIn, none }
class AuthController = _AuthControllerBase with _$AuthController;

abstract class _AuthControllerBase with Store {
_AuthControllerBase(this.accountStorage, this.messagesStorage);
final AccountStorage accountStorage;
final MessagesStorage messagesStorage;
_AuthControllerBase(this._accountStorage, this._messagesStorage);
final AccountStorage _accountStorage;
final MessagesStorage _messagesStorage;

@observable
Observable<AuthState> authState = Observable(AuthState.none);

@observable
Observable<LocalAccount?> account = Observable(null);

@computed
bool get isLoggedIn => authState.value == AuthState.loggedIn;

@action
Future<void> init() async {
final isLoggedIn = await accountStorage.isLoggedIn();
final isLoggedIn = await _accountStorage.isLoggedIn();

if (isLoggedIn) {
authState.value = AuthState.loggedIn;
account.value = await accountStorage.getAccount();
account.value = await _accountStorage.getAccount();
}
}

@action
Future<void> logout() async {
await accountStorage.removeAccount();
await messagesStorage.clear();
await _accountStorage.removeAccount();
await _messagesStorage.clear();

account.value = null;
authState.value = AuthState.none;
Expand Down
14 changes: 14 additions & 0 deletions lib/core/services/router/guards/auth.guard.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:beamer/beamer.dart';
import 'package:spamify/core/auth/controllers/auth.controller.dart';
import 'package:spamify/core/services/di.service.dart';
import 'package:spamify/core/services/router/router.service.dart';

class AuthGuard extends BeamGuard {
AuthGuard()
: super(
pathPatterns: [RouterMeta.settings.path],
guardNonMatching: false,
check: (_, __) =>
getIt<AuthController>().authState.value == AuthState.loggedIn,
beamToNamed: (_, __) => RouterMeta.fetchEmailAddress.path);
}
4 changes: 4 additions & 0 deletions lib/core/services/router/router.service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:beamer/beamer.dart';
import 'package:flutter/material.dart';
import 'package:spamify/core/services/di.service.dart';
import 'package:spamify/core/services/router/guards/auth.guard.dart';
import 'package:spamify/core/services/router/router.controller.dart';
import 'package:spamify/features/app/views/app.view.dart';
import 'package:spamify/features/home/views/home.view.dart';
Expand Down Expand Up @@ -45,6 +46,9 @@ final appRouterDelegate = BeamerDelegate(

final homeRouterKey = GlobalKey<BeamerState>();
final homeRouterDelegate = BeamerDelegate(
guards: [
AuthGuard(),
],
initialPath: RouterMeta.fetchEmailAddress.path,
locationBuilder: RoutesLocationBuilder(
routes: {
Expand Down
11 changes: 11 additions & 0 deletions lib/features/app/controller/app.controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ abstract class _AppViewControllerBase with Store {
if (authController.authState.value == AuthState.loggedIn) {
tabs = [...tabs, ..._authenticationTabs];
}
if (authController.authState.value == AuthState.none) {
tabs = [..._rawTabs];
}
});
}

Expand All @@ -49,6 +52,14 @@ abstract class _AppViewControllerBase with Store {
),
];

final List<SidebarItemWithRouter> _rawTabs = [
SidebarItemWithRouter(
initialLocation: RouterMeta.fetchEmailAddress.path,
label: const Text("Get new email address"),
icon: CupertinoIcons.mail,
),
];

final List<SidebarItemWithRouter> _authenticationTabs = [
SidebarItemWithRouter(
initialLocation: RouterMeta.inbox.path,
Expand Down
23 changes: 3 additions & 20 deletions lib/features/home/controllers/home.controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,15 @@ abstract class _HomeViewControllerBase with Store {
if (value.newValue == AuthState.loggedIn) {
textFieldController.text = authController.account.value?.address ?? "";
}
});
authController.account.observe((value) {
if (value.newValue != null) {
textFieldController.text = authController.account.value?.address ?? "";
if (value.newValue == AuthState.none) {
textFieldController.text = "";
}
});
}

@action
Future<void> fetchNewAdress() async {
isLoading = true;
// try {
final password = generateRandomString(10);

final domains = await accountRepository.fetchDomains();
Expand All @@ -59,25 +56,11 @@ abstract class _HomeViewControllerBase with Store {
address: _account.address, password: password, token: token.token);

await accountStorage.saveAccount(authController.account.value!);
authController.authState.value = AuthState.loggedIn;

textFieldController.text = authController.account.value?.address ?? "";

isLoading = false;
// } catch (e) {
// print(e);
// }
}

@action
Future<void> login(String address, String password) async {
isLoading = true;
try {
final _account = await accountRepository.login(address, password);
final account = LocalAccount(
address: address, password: password, token: _account.token);
} catch (e) {
rethrow;
}
}

@action
Expand Down
83 changes: 57 additions & 26 deletions lib/features/home/views/home.view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:macos_ui/macos_ui.dart';
import 'package:spamify/core/auth/controllers/auth.controller.dart';
import 'package:spamify/features/home/controllers/home.controller.dart';
import 'package:spamify/core/services/di.service.dart';

Expand All @@ -17,6 +18,7 @@ class HomeView extends StatefulWidget {

class _HomeViewState extends State<HomeView> {
final HomeViewController controller = getIt<HomeViewController>();
final AuthController authController = getIt<AuthController>();

@override
void initState() {
Expand Down Expand Up @@ -120,13 +122,19 @@ class _HomeViewState extends State<HomeView> {
children: [
SizedBox(
width: 300,
child: MacosTextField(
controller: controller.textFieldController,
readOnly: true,
style: const TextStyle(
fontSize: 15,
),
),
child: Observer(builder: (_) {
if (!authController.isLoggedIn) {
return const SizedBox.shrink();
}

return MacosTextField(
controller: controller.textFieldController,
readOnly: true,
style: const TextStyle(
fontSize: 15,
),
);
}),
),
_copyButton()
],
Expand Down Expand Up @@ -155,20 +163,27 @@ class _HomeViewState extends State<HomeView> {
}

Widget _copyButton() {
return PushButton(
buttonSize: ButtonSize.small,
color: Colors.transparent,
onPressed: () => controller.copyEmailAddress(),
child: Observer(builder: (_) {
return AnimatedCrossFade(
firstChild: const MacosIcon(CupertinoIcons.doc_on_clipboard),
secondChild: const MacosIcon(CupertinoIcons.doc_on_clipboard_fill),
crossFadeState: controller.copied
? CrossFadeState.showSecond
: CrossFadeState.showFirst,
duration: const Duration(milliseconds: 700));
}),
);
return Observer(builder: (context) {
if (!authController.isLoggedIn) {
return const SizedBox.shrink();
}

return PushButton(
buttonSize: ButtonSize.small,
color: Colors.transparent,
onPressed: () => controller.copyEmailAddress(),
child: Observer(builder: (_) {
return AnimatedCrossFade(
firstChild: const MacosIcon(CupertinoIcons.doc_on_clipboard),
secondChild:
const MacosIcon(CupertinoIcons.doc_on_clipboard_fill),
crossFadeState: controller.copied
? CrossFadeState.showSecond
: CrossFadeState.showFirst,
duration: const Duration(milliseconds: 700));
}),
);
});
}

ToolBar _toolBar() {
Expand All @@ -179,11 +194,27 @@ class _HomeViewState extends State<HomeView> {
color: MacosTheme.of(context).pushButtonTheme.disabledColor,
onPressed: () => MacosWindowScope.of(context).toggleSidebar()),
actions: [
ToolBarIconButton(
label: "",
showLabel: false,
icon: const MacosIcon(CupertinoIcons.settings),
onPressed: () => context.beamToNamed("/fetch-email-adress/settings"),
CustomToolbarItem(
inToolbarBuilder: (context) {
return Observer(
builder: (context) {
if (authController.authState.value == AuthState.none) {
return const SizedBox.shrink();
}

return ToolBarIconButton(
label: "",
showLabel: false,
tooltipMessage: "Settings",
icon: const MacosIcon(
CupertinoIcons.settings,
),
onPressed: () =>
context.beamToNamed("/fetch-email-adress/settings"),
).build(context, ToolbarItemDisplayMode.inToolbar);
},
);
},
),
],
title: const Text("Fetch new account"),
Expand Down
11 changes: 10 additions & 1 deletion lib/features/settings/controllers/settings.controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:spamify/config.dart';
import 'package:spamify/core/auth/controllers/auth.controller.dart';
import 'package:spamify/core/constants/theme.dart';
import 'package:spamify/core/exstensions/toast.extension.dart';
import 'package:spamify/core/services/router/router.service.dart';
import 'package:spamify/core/storage/account.storage.dart';
import 'package:spamify/core/storage/messages.storage.dart';
import 'package:spamify/features/settings/models/update.model.dart';
Expand Down Expand Up @@ -40,6 +41,14 @@ abstract class _SettingsViewControllerBase with Store {
Future<void> initState() async {
await _getVersion();
await checkForUpdates();

authController.authState.observe((value) {
if (value.newValue != null) {
if (value.newValue == AuthState.none) {
appRouterDelegate.beamToNamed(RouterMeta.fetchEmailAddress.path);
}
}
});
}

Future<void> _getVersion() async {
Expand Down Expand Up @@ -82,7 +91,7 @@ abstract class _SettingsViewControllerBase with Store {
print("Update available");
break;
case 0:
print("No update available");
_toast.showToast("Update not found.");
break;
case 1:
print("Higher version available");
Expand Down

0 comments on commit baa60a1

Please sign in to comment.