app/lib/data/auth.dart

53 lines
1.2 KiB
Dart
Raw Normal View History

2022-04-28 17:54:56 +07:00
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
2022-04-28 17:54:56 +07:00
import 'package:nekoya_app/api/api.dart';
2022-05-16 11:36:07 +07:00
2022-05-12 18:16:41 +07:00
String getEncodedSession(userId, sessionToken) {
var sessionData = {"user_id": userId, "session_token": sessionToken};
2022-04-28 17:54:56 +07:00
var sessionRaw = jsonEncode(sessionData).toString();
var sessionEncoded = base64.encode(utf8.encode(sessionRaw));
return sessionEncoded;
}
Future<void> addSession(userId, sessionToken) async {
final prefs = await SharedPreferences.getInstance();
2022-05-12 18:16:41 +07:00
String session = getEncodedSession(userId, sessionToken);
await prefs.setString('session', session);
}
2022-05-12 18:13:10 +07:00
Future<bool> checkSessionExist() async {
final prefs = await SharedPreferences.getInstance();
if (prefs.containsKey('session')) {
2022-05-16 11:36:07 +07:00
var res = await getSessions(await getSession());
if (res['statusCode'] == 200) {
return true;
2022-05-23 01:16:45 +07:00
} else {
removeSession();
return false;
2022-05-16 11:36:07 +07:00
}
2022-05-12 18:13:10 +07:00
}
return false;
}
2022-05-12 21:44:58 +07:00
Future<String> getSession() async {
final prefs = await SharedPreferences.getInstance();
if (prefs.containsKey('session')) {
return prefs.getString('session') ?? 'null';
}
return 'null';
}
Future<void> removeSession() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove('session');
}