43 lines
928 B
Dart
43 lines
928 B
Dart
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._();
|
|
}
|