From 0cf36a53fdb5607827aad0723901b2790a52c1af Mon Sep 17 00:00:00 2001 From: Moe Poi ~ Date: Fri, 10 Nov 2023 11:21:09 +0700 Subject: [PATCH] Use Hive for save auth data --- lib/data/auth.dart | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/data/auth.dart b/lib/data/auth.dart index 1afce20..eb7092d 100644 --- a/lib/data/auth.dart +++ b/lib/data/auth.dart @@ -1,5 +1,5 @@ import 'dart:convert'; -import 'package:shared_preferences/shared_preferences.dart'; +import 'package:hive/hive.dart'; import 'package:nekoya_app/api/api.dart'; @@ -11,17 +11,17 @@ String getEncodedSession(userId, sessionToken) { } Future addSession(userId, sessionToken) async { - final prefs = await SharedPreferences.getInstance(); + final box = Hive.box(); String session = getEncodedSession(userId, sessionToken); - await prefs.setString('session', session); + box.put('session', session); } Future checkSessionExist() async { - final prefs = await SharedPreferences.getInstance(); + final box = Hive.box(); - if (prefs.containsKey('session')) { + if (box.get('session') != null) { var res = await getSessions(await getSession()); if (res['statusCode'] == 200) { @@ -36,17 +36,17 @@ Future checkSessionExist() async { } Future getSession() async { - final prefs = await SharedPreferences.getInstance(); + final box = Hive.box(); - if (prefs.containsKey('session')) { - return prefs.getString('session') ?? 'null'; + if (box.get('session') != null) { + return box.get('session', defaultValue: 'null'); } return 'null'; } Future removeSession() async { - final prefs = await SharedPreferences.getInstance(); + final box = Hive.box(); - await prefs.remove('session'); + box.delete('session'); }