OpenMediacenterMobileFlutter/lib/api/token.dart

61 lines
1.5 KiB
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 '../log/log.dart';
2021-12-10 10:40:20 +00:00
class TokenT {
String token;
String domain;
2022-09-02 16:42:38 +00:00
String videoPath;
2022-09-02 16:42:38 +00:00
TokenT(this.token, this.domain, this.videoPath);
}
2021-12-10 10:40:20 +00:00
class Token {
static final Token _token = Token._();
final _storage = const FlutterSecureStorage();
String _tokenval = "";
String _domain = "";
2022-09-02 16:42:38 +00:00
String _vPath = "";
2021-12-10 10:40:20 +00:00
static Token getInstance() {
return _token;
}
Future<TokenT?> getToken() async {
2022-09-02 16:42:38 +00:00
if (_tokenval == "" || _domain == "" || _vPath == "") {
Log.d("reading token store");
WidgetsFlutterBinding.ensureInitialized();
final token = await _storage.read(key: 'jwt');
final domain = await _storage.read(key: 'domain');
2022-09-02 16:42:38 +00:00
final vPath = await _storage.read(key: 'videoPath');
// check if value is defined in phone store
2022-09-02 16:42:38 +00:00
if (token != null && domain != null && vPath != null) {
_tokenval = token;
_domain = domain;
2022-09-02 16:42:38 +00:00
return TokenT(token, domain, vPath);
} else {
Log.d("no token defined");
2022-09-02 16:42:38 +00:00
return null;
}
2021-12-10 10:40:20 +00:00
} else {
2022-09-02 16:42:38 +00:00
return TokenT(_tokenval, _domain, _vPath);
2021-12-10 10:40:20 +00:00
}
}
2022-09-02 16:42:38 +00:00
void setToken(String token, String domain, String videoPath) {
2021-12-10 10:40:20 +00:00
_tokenval = token;
_domain = domain;
2022-09-02 16:42:38 +00:00
_vPath = videoPath;
2021-12-10 10:40:20 +00:00
_storage.write(key: 'jwt', value: token);
_storage.write(key: 'domain', value: domain);
2022-09-02 16:42:38 +00:00
_storage.write(key: 'videoPath', value: videoPath);
2021-12-10 10:40:20 +00:00
}
Token._();
}