forked from ml-opensource/flutter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ml-opensource#80): Introduce
AuthTokenStorage
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
lib/data/api/new/auth_token_storage/auth_token_storage.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'package:flutter_template/data/model/auth/auth_tokens.dart'; | ||
|
||
/// A storage for authentication tokens. | ||
abstract class AuthTokenStorage { | ||
// Future<String?> get accessToken; | ||
// Future<String?> get refreshToken; | ||
// Future<double?> get tokenExpiryTime; | ||
|
||
// Future<bool> hasValidAccessToken(); | ||
|
||
// Future setAccessToken(String token); | ||
// Future setRefreshToken(String token); | ||
// Future setRefreshTokenExpiry(double expiresIn); | ||
|
||
Future<AuthTokens?> get(); | ||
Future<void> set(AuthTokens tokens); | ||
|
||
Future<void> clear(); | ||
} |
44 changes: 44 additions & 0 deletions
44
lib/data/api/new/auth_token_storage/secure_auth_token_storage.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | ||
import 'package:flutter_template/data/api/new/auth_token_storage/auth_token_storage.dart'; | ||
import 'package:flutter_template/data/model/auth/auth_tokens.dart'; | ||
import 'package:injectable/injectable.dart'; | ||
|
||
/// A storage for auth tokens that uses [FlutterSecureStorage]. | ||
@LazySingleton(as: AuthTokenStorage) | ||
class SecureAuthTokenStorage extends AuthTokenStorage { | ||
static const String _prefix = 'auth'; | ||
|
||
@visibleForTesting | ||
static const String tokenKey = '${_prefix}_token'; | ||
|
||
SecureAuthTokenStorage(this._storage); | ||
|
||
final FlutterSecureStorage _storage; | ||
|
||
@override | ||
Future<AuthTokens?> get() async { | ||
final json = await _storage.read(key: tokenKey); | ||
|
||
if (json == null) { | ||
return null; | ||
} | ||
|
||
final map = jsonDecode(json) as Map<String, dynamic>; | ||
|
||
return AuthTokens.fromJson(map); | ||
} | ||
|
||
@override | ||
Future<void> set(AuthTokens tokens) { | ||
final json = jsonEncode(tokens.toJson()); | ||
return _storage.write(key: tokenKey, value: json); | ||
} | ||
|
||
@override | ||
Future<void> clear() { | ||
return _storage.delete(key: tokenKey); | ||
} | ||
} |