-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathauth_store.dart
75 lines (56 loc) · 2.02 KB
/
auth_store.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import "dart:async";
import "dart:convert";
import "./dtos/record_model.dart";
/// Event object that holds an AuthStore state.
class AuthStoreEvent {
AuthStoreEvent(this.token, this.record);
final String token;
final RecordModel? record;
@Deprecated("use record")
dynamic get model => record as dynamic;
@override
String toString() => "token: $token\nrecord: $record";
}
/// Base authentication store management service that keep tracks of
/// the authenticated User/Admin model and its token.
class AuthStore {
final _onChangeController = StreamController<AuthStoreEvent>.broadcast();
String _token = "";
RecordModel? _record;
/// Returns the saved auth token (if any).
String get token => _token;
/// Returns the saved auth record (if any).
RecordModel? get record => _record;
@Deprecated("use record")
dynamic get model => record as dynamic;
/// Stream that gets triggered on each auth store change
/// (aka. on [save()] and [clear()] call).
Stream<AuthStoreEvent> get onChange => _onChangeController.stream;
/// Loosely checks if the current AuthStore has valid auth data
/// (eg. whether the token is expired or not).
bool get isValid {
final parts = token.split(".");
if (parts.length != 3) {
return false;
}
final tokenPart = base64.normalize(parts[1]);
final data = jsonDecode(utf8.decode(base64Decode(tokenPart)))
as Map<String, dynamic>;
final exp = data["exp"] is int
? data["exp"] as int
: (int.tryParse(data["exp"].toString()) ?? 0);
return exp > (DateTime.now().millisecondsSinceEpoch / 1000);
}
/// Saves the provided [newToken] and [newRecord] auth data into the store.
void save(String newToken, RecordModel? newRecord) {
_token = newToken;
_record = newRecord;
_onChangeController.add(AuthStoreEvent(token, newRecord));
}
/// Clears the previously stored [token] and [record] auth data.
void clear() {
_token = "";
_record = null;
_onChangeController.add(AuthStoreEvent(token, record));
}
}