1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 15:03:22 +00:00
comunicmobile/lib/helpers/account_credentials_helper.dart

34 lines
967 B
Dart
Raw Normal View History

2019-04-22 17:16:26 +00:00
import 'dart:convert';
import 'package:comunic/models/login_tokens.dart';
import 'package:shared_preferences/shared_preferences.dart';
2019-04-21 08:34:27 +00:00
/// Accounts credentials helper
///
/// Stores current account tokens
///
/// @author Pierre HUBERT
class AccountCredentialsHelper {
2019-04-22 17:16:26 +00:00
/// Set new login tokens
Future<void> set(LoginTokens tokens) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
2019-04-23 09:48:49 +00:00
await prefs.setString(
"login_tokens", tokens == null ? "null" : tokens.toString());
2019-04-21 08:34:27 +00:00
}
2019-04-22 17:16:26 +00:00
/// 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");
2019-04-23 09:48:49 +00:00
if (string == null || string == "null") return null;
2019-04-22 17:16:26 +00:00
return LoginTokens.fromJSON(jsonDecode(string));
2019-04-23 09:48:49 +00:00
} on Exception catch (e) {
2019-04-22 17:16:26 +00:00
print(e.toString());
return null;
}
}
2019-04-23 09:48:49 +00:00
}