OpenMediacenterMobileFlutter/lib/api/token.dart

43 lines
928 B
Dart
Raw Normal View History

2021-12-10 10:40:20 +00:00
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:logger/logger.dart';
class Token {
static final Token _token = Token._();
final _storage = const FlutterSecureStorage();
final _l = Logger();
String _tokenval = "";
static Token getInstance() {
return _token;
}
Future<String?> _readToken() async {
_l.d("reading token store");
WidgetsFlutterBinding.ensureInitialized();
return _storage.read(key: 'jwt');
}
Future<String> getToken() async {
var completer = Completer<String>();
if (_tokenval == "") {
final token = await _readToken();
completer.complete(token);
} else {
completer.complete(_tokenval);
}
return completer.future;
}
void setToken(String token) {
_tokenval = token;
_storage.write(key: 'jwt', value: token);
}
Token._();
}