mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-29 08:16:28 +00:00
34 lines
967 B
Dart
34 lines
967 B
Dart
import 'dart:convert';
|
|
|
|
import 'package:comunic/models/login_tokens.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
/// Accounts credentials helper
|
|
///
|
|
/// Stores current account tokens
|
|
///
|
|
/// @author Pierre HUBERT
|
|
|
|
class AccountCredentialsHelper {
|
|
|
|
/// Set new login tokens
|
|
Future<void> set(LoginTokens tokens) async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(
|
|
"login_tokens", tokens == null ? "null" : tokens.toString());
|
|
}
|
|
|
|
/// Get current [LoginTokens]. Returns null if none or in case of failure
|
|
Future<LoginTokens> get() async {
|
|
try {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
final string = prefs.getString("login_tokens");
|
|
if (string == null || string == "null") return null;
|
|
return LoginTokens.fromJSON(jsonDecode(string));
|
|
} on Exception catch (e) {
|
|
print(e.toString());
|
|
return null;
|
|
}
|
|
}
|
|
}
|