Commit 860b6510 authored by Ilham Maulana's avatar Ilham Maulana 💻

fix: handle login session on android

parent 9cd799d3
import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';
import 'package:go_router/go_router.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:library_app/src/screens/profile_edit_screen.dart';
......@@ -12,8 +13,11 @@ import 'package:library_app/src/providers/book_provider.dart';
import 'package:library_app/src/screens/form_screen.dart';
import 'package:library_app/src/screens/list/list_screen.dart';
void main() {
runApp(const LibraryApp());
void main() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final isAuthenticated = prefs.getString('access_token') != null;
runApp(isAuthenticated ? const LibraryApp() : const LoginScreen());
}
class LibraryApp extends StatefulWidget {
......@@ -32,17 +36,11 @@ class _LibraryApp extends State<LibraryApp> {
routes: [
GoRoute(
path: '/',
builder: (context, state) =>
Provider.of<AuthProvider>(context).isAuthenticated
? const ListScreen()
: const LoginScreen(),
builder: (context, state) => const ListScreen(),
),
GoRoute(
path: '/sign-up',
builder: (context, state) =>
Provider.of<AuthProvider>(context).isAuthenticated
? const ListScreen()
: const SignUpScreen(),
builder: (context, state) => const SignUpScreen(),
),
GoRoute(
path: '/reset-password',
......
import 'dart:convert';
import 'package:flutter/widgets.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart' as http;
......@@ -9,13 +9,10 @@ import 'package:library_app/src/models/token.dart';
import 'package:library_app/src/models/user.dart';
class AuthProvider with ChangeNotifier {
final storage = const FlutterSecureStorage();
String baseUrl = 'https://ilhammaulana.pythonanywhere.com/api/v1';
String? message;
User? user;
bool isAuthenticated = false;
bool invalidUsernameOrPassword = false;
List<dynamic>? memberLoans;
......@@ -38,11 +35,13 @@ class AuthProvider with ChangeNotifier {
List<dynamic>? overduedLoans;
Future<void> storeAccessToken(String accessToken) async {
await storage.write(key: 'access_token', value: accessToken);
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('access_token', accessToken);
}
Future<String?> getAccessToken() async {
return await storage.read(key: 'access_token');
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString('access_token');
}
void setLoading(bool value) {
......@@ -67,7 +66,6 @@ class AuthProvider with ChangeNotifier {
overduedLoans = null;
totalPages = null;
isAuthenticated = false;
invalidUsernameOrPassword = false;
filterByUpcoming = false;
filterByOverdued = false;
......@@ -100,7 +98,6 @@ class AuthProvider with ChangeNotifier {
String token = Token.fromJson(data)!.key;
await storeAccessToken(token);
isAuthenticated = true;
setInvalidUsernameOrPassword(false);
debugPrint("Login successful $token");
......@@ -133,7 +130,8 @@ class AuthProvider with ChangeNotifier {
);
if (response.statusCode == 200) {
await storage.delete(key: 'token');
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.remove('access_token');
resetAllState();
} else {
debugPrint("Logout failed: ${response.statusCode} ${response.body}");
......@@ -165,7 +163,6 @@ class AuthProvider with ChangeNotifier {
final data = jsonDecode(response.body);
String token = Token.fromJson(data)!.key;
storeAccessToken(token);
isAuthenticated = true;
message = null;
if (context.mounted) {
......@@ -482,7 +479,7 @@ class AuthProvider with ChangeNotifier {
// for admin or librarian
Future<void> getLoans(String? type) async {
final token = await storage.read(key: 'access_token');
final token = await getAccessToken();
String url = "$baseUrl/book-loans";
if (type == "upcoming") {
url += '?near_outstanding=True';
......
......@@ -6,10 +6,6 @@
#include "generated_plugin_registrant.h"
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin");
flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar);
}
......@@ -3,7 +3,6 @@
#
list(APPEND FLUTTER_PLUGIN_LIST
flutter_secure_storage_linux
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST
......
......@@ -5,10 +5,10 @@
import FlutterMacOS
import Foundation
import flutter_secure_storage_macos
import path_provider_foundation
import shared_preferences_foundation
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}
......@@ -81,6 +81,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.3"
file:
dependency: transitive
description:
name: file
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
flutter:
dependency: "direct main"
description: flutter
......@@ -94,54 +102,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.0"
flutter_secure_storage:
dependency: "direct main"
description:
name: flutter_secure_storage
sha256: "165164745e6afb5c0e3e3fcc72a012fb9e58496fb26ffb92cf22e16a821e85d0"
url: "https://pub.dev"
source: hosted
version: "9.2.2"
flutter_secure_storage_linux:
dependency: transitive
description:
name: flutter_secure_storage_linux
sha256: "4d91bfc23047422cbcd73ac684bc169859ee766482517c22172c86596bf1464b"
url: "https://pub.dev"
source: hosted
version: "1.2.1"
flutter_secure_storage_macos:
dependency: transitive
description:
name: flutter_secure_storage_macos
sha256: "1693ab11121a5f925bbea0be725abfcfbbcf36c1e29e571f84a0c0f436147a81"
url: "https://pub.dev"
source: hosted
version: "3.1.2"
flutter_secure_storage_platform_interface:
dependency: transitive
description:
name: flutter_secure_storage_platform_interface
sha256: cf91ad32ce5adef6fba4d736a542baca9daf3beac4db2d04be350b87f69ac4a8
url: "https://pub.dev"
source: hosted
version: "1.1.2"
flutter_secure_storage_web:
dependency: transitive
description:
name: flutter_secure_storage_web
sha256: f4ebff989b4f07b2656fb16b47852c0aab9fed9b4ec1c70103368337bc1886a9
url: "https://pub.dev"
source: hosted
version: "1.2.1"
flutter_secure_storage_windows:
dependency: transitive
description:
name: flutter_secure_storage_windows
sha256: b20b07cb5ed4ed74fc567b78a72936203f587eba460af1df11281c9326cd3709
url: "https://pub.dev"
source: hosted
version: "3.1.2"
flutter_svg:
dependency: "direct main"
description:
......@@ -200,14 +160,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.19.0"
js:
dependency: transitive
description:
name: js
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
url: "https://pub.dev"
source: hosted
version: "0.6.7"
leak_tracker:
dependency: transitive
description:
......@@ -376,6 +328,62 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.1.2"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
sha256: c272f9cabca5a81adc9b0894381e9c1def363e980f960fa903c604c471b22f68
url: "https://pub.dev"
source: hosted
version: "2.3.1"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
sha256: a7e8467e9181cef109f601e3f65765685786c1a738a83d7fbbde377589c0d974
url: "https://pub.dev"
source: hosted
version: "2.3.1"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
sha256: c4b35f6cb8f63c147312c054ce7c2254c8066745125264f0c88739c417fc9d9f
url: "https://pub.dev"
source: hosted
version: "2.5.2"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
sha256: "580abfd40f415611503cae30adf626e6656dfb2f0cee8f465ece7b6defb40f2f"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
sha256: "57cbf196c486bc2cf1f02b85784932c6094376284b3ad5779d1b1c6c6a816b80"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
sha256: d2ca4132d3946fec2184261726b355836a82c33d7d5b67af32692aff18a4684e
url: "https://pub.dev"
source: hosted
version: "2.4.2"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
sha256: "94ef0f72b2d71bc3e700e025db3710911bd51a71cefb65cc609dd0d9a982e3c1"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
sky_engine:
dependency: transitive
description: flutter
......@@ -485,14 +493,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.0"
win32:
dependency: transitive
description:
name: win32
sha256: "68d1e89a91ed61ad9c370f9f8b6effed9ae5e0ede22a270bdfa6daf79fc2290a"
url: "https://pub.dev"
source: hosted
version: "5.5.4"
xdg_directories:
dependency: transitive
description:
......
......@@ -40,8 +40,8 @@ dependencies:
intl: ^0.19.0
provider: ^6.1.2
http: ^1.2.2
flutter_secure_storage: ^9.2.2
go_router: ^14.2.1
shared_preferences: ^2.3.1
dev_dependencies:
flutter_test:
......
......@@ -6,9 +6,6 @@
#include "generated_plugin_registrant.h"
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
void RegisterPlugins(flutter::PluginRegistry* registry) {
FlutterSecureStorageWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin"));
}
......@@ -3,7 +3,6 @@
#
list(APPEND FLUTTER_PLUGIN_LIST
flutter_secure_storage_windows
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment