From 01d5baaf301de31d21dc264c80e26736bb03a633 Mon Sep 17 00:00:00 2001 From: Samuel Date: Mon, 9 Dec 2024 15:30:37 +0100 Subject: [PATCH] feat(logout): added button to logout in the dashboard --- .../lib/pages/dashboard/dashboard.dart | 21 ++++++++++++++----- .../lib/services/login/auth_service.dart | 10 +++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/client_mobile/lib/pages/dashboard/dashboard.dart b/client_mobile/lib/pages/dashboard/dashboard.dart index bca4705..ef1e271 100644 --- a/client_mobile/lib/pages/dashboard/dashboard.dart +++ b/client_mobile/lib/pages/dashboard/dashboard.dart @@ -1,4 +1,6 @@ +import 'package:client_mobile/services/login/auth_service.dart'; import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; class DashboardPage extends StatefulWidget { const DashboardPage({super.key}); @@ -10,10 +12,19 @@ class DashboardPage extends StatefulWidget { class _DashboardPageState extends State { @override Widget build(BuildContext context) { - return const Scaffold( - body: Center( - child: Text("Dashboard ") - ) + return Scaffold( + body: const Center( + child: Text("Dashboard "), + ), + floatingActionButton: FloatingActionButton( + onPressed: () async { + bool hasLogout = await AuthService.logout(); + if (hasLogout) + context.pushReplacement("/login"); + }, + tooltip: 'Logout', + child: const Icon(Icons.login), + ), ); } -} \ No newline at end of file +} diff --git a/client_mobile/lib/services/login/auth_service.dart b/client_mobile/lib/services/login/auth_service.dart index fed1480..29fdf6e 100644 --- a/client_mobile/lib/services/login/auth_service.dart +++ b/client_mobile/lib/services/login/auth_service.dart @@ -177,4 +177,14 @@ class AuthService { return (false); } } + + static Future logout() async { + bool isLogin = await secureStorage.read(key: "bearer_token") != null; + + if (isLogin) { + await secureStorage.delete(key: "bearer_token"); + return (true); + } + return (false); + } }