app/lib/data/auth.dart

53 lines
1.1 KiB
Dart
Raw Normal View History

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
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) {
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;
}
Future<void> addSession(userId, sessionToken) async {
2023-11-10 05:21:09 +01:00
final box = Hive.box();
2022-05-12 13:16:41 +02:00
String session = getEncodedSession(userId, sessionToken);
2023-11-10 05:21:09 +01:00
box.put('session', session);
}
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';
}
Future<void> removeSession() async {
2023-11-10 05:21:09 +01:00
final box = Hive.box();
2023-11-10 05:21:09 +01:00
box.delete('session');
}