mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 09:45:22 +00:00
54 lines
1.5 KiB
Dart
54 lines
1.5 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:syncrow_app/utils/helpers/decode_base64.dart';
|
|
|
|
class Token {
|
|
static const String loginAccessTokenKey = 'access_token';
|
|
static const String loginRefreshTokenKey = 'refreshToken';
|
|
|
|
final String accessToken;
|
|
final String refreshToken;
|
|
|
|
Token.emptyConstructor()
|
|
: accessToken = '',
|
|
refreshToken = '';
|
|
|
|
bool get accessTokenIsNotEmpty => accessToken.isNotEmpty;
|
|
|
|
bool get refreshTokenIsNotEmpty => refreshToken.isNotEmpty;
|
|
|
|
bool get isNotEmpty => accessToken.isNotEmpty && refreshToken.isNotEmpty;
|
|
|
|
Token(
|
|
this.accessToken,
|
|
this.refreshToken,
|
|
);
|
|
|
|
Token.refreshToken(this.refreshToken) : accessToken = '';
|
|
|
|
factory Token.fromJson(Map<String, dynamic> json) {
|
|
//save token to secure storage
|
|
var storage = const FlutterSecureStorage();
|
|
storage.write(
|
|
key: loginAccessTokenKey, value: json[loginAccessTokenKey] ?? '');
|
|
|
|
//create token object ?
|
|
return Token(
|
|
json[loginAccessTokenKey] ?? '', json[loginRefreshTokenKey] ?? '');
|
|
}
|
|
|
|
Map<String, String> toJson() => {loginRefreshTokenKey: refreshToken};
|
|
|
|
Map<String, String> accessTokenToJson() => {loginAccessTokenKey: accessToken};
|
|
|
|
Map<String, dynamic> decodeToken() {
|
|
final parts = accessToken.split('.');
|
|
if (parts.length != 3) {
|
|
throw Exception('invalid access token');
|
|
}
|
|
final payload = decodeBase64(parts[1]);
|
|
return json.decode(payload);
|
|
}
|
|
}
|