2022-04-28 12:54:56 +02:00
|
|
|
import 'dart:convert';
|
2023-11-10 05:21:09 +01:00
|
|
|
import 'package:hive/hive.dart';
|
2022-04-28 12:54:56 +02:00
|
|
|
|
2023-09-22 09:29:07 +02:00
|
|
|
import 'package:nekoya_app/api/api.dart';
|
2022-05-16 06:36:07 +02:00
|
|
|
|
2022-05-12 13:16:41 +02:00
|
|
|
String getEncodedSession(userId, sessionToken) {
|
2023-09-22 09:29:07 +02:00
|
|
|
var sessionData = {"user_id": userId, "session_token": sessionToken};
|
2022-04-28 12:54:56 +02:00
|
|
|
var sessionRaw = jsonEncode(sessionData).toString();
|
|
|
|
var sessionEncoded = base64.encode(utf8.encode(sessionRaw));
|
|
|
|
return sessionEncoded;
|
2022-04-28 12:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> addSession(userId, sessionToken) async {
|
2023-11-10 05:21:09 +01:00
|
|
|
final box = Hive.box();
|
2022-04-28 12:55:46 +02:00
|
|
|
|
2022-05-12 13:16:41 +02:00
|
|
|
String session = getEncodedSession(userId, sessionToken);
|
2022-04-28 12:55:46 +02:00
|
|
|
|
2023-11-10 05:21:09 +01:00
|
|
|
box.put('session', session);
|
2022-04-28 12:55:46 +02:00
|
|
|
}
|
|
|
|
|
2022-05-12 13:13:10 +02:00
|
|
|
Future<bool> checkSessionExist() async {
|
2023-11-10 05:21:09 +01:00
|
|
|
final box = Hive.box();
|
2022-05-12 13:13:10 +02:00
|
|
|
|
2023-11-10 05:21:09 +01:00
|
|
|
if (box.get('session') != null) {
|
2022-05-16 06:36:07 +02:00
|
|
|
var res = await getSessions(await getSession());
|
|
|
|
|
|
|
|
if (res['statusCode'] == 200) {
|
|
|
|
return true;
|
2022-05-22 20:16:45 +02:00
|
|
|
} else {
|
|
|
|
removeSession();
|
|
|
|
return false;
|
2022-05-16 06:36:07 +02:00
|
|
|
}
|
2022-05-12 13:13:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:44:58 +02:00
|
|
|
Future<String> getSession() async {
|
2023-11-10 05:21:09 +01:00
|
|
|
final box = Hive.box();
|
2022-05-12 16:44:58 +02:00
|
|
|
|
2023-11-10 05:21:09 +01:00
|
|
|
if (box.get('session') != null) {
|
|
|
|
return box.get('session', defaultValue: 'null');
|
2022-05-12 16:44:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 'null';
|
|
|
|
}
|
|
|
|
|
2022-04-28 12:55:46 +02:00
|
|
|
Future<void> removeSession() async {
|
2023-11-10 05:21:09 +01:00
|
|
|
final box = Hive.box();
|
2022-04-28 12:55:46 +02:00
|
|
|
|
2023-11-10 05:21:09 +01:00
|
|
|
box.delete('session');
|
2023-09-22 09:29:07 +02:00
|
|
|
}
|