OpenMediacenterMobileFlutter/lib/api/token.dart

59 lines
1.3 KiB
Dart

import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import '../log/log.dart';
class TokenT {
String token;
String domain;
TokenT(this.token, this.domain);
}
class Token {
static final Token _token = Token._();
final _storage = const FlutterSecureStorage();
String _tokenval = "";
String _domain = "";
static Token getInstance() {
return _token;
}
Future<TokenT?> getToken() async {
var completer = Completer<TokenT?>();
if (_tokenval == "" || _domain == "") {
Log.d("reading token store");
WidgetsFlutterBinding.ensureInitialized();
final token = await _storage.read(key: 'jwt');
final domain = await _storage.read(key: 'domain');
// check if value is defined in phone store
if (token != null && domain != null) {
_tokenval = token;
_domain = domain;
completer.complete(TokenT(token, domain));
} else {
Log.d("no token defined");
completer.complete(null);
}
} else {
completer.complete(TokenT(_tokenval, _domain));
}
return completer.future;
}
void setToken(String token, String domain) {
_tokenval = token;
_domain = domain;
_storage.write(key: 'jwt', value: token);
_storage.write(key: 'domain', value: domain);
}
Token._();
}