|
| 1 | +import 'dart:convert'; |
| 2 | + |
1 | 3 | import 'package:hive_flutter/hive_flutter.dart'; |
2 | 4 |
|
| 5 | +import '../../../domain/models/user.dart'; |
3 | 6 | import '../../constants/strings.dart'; |
4 | 7 |
|
5 | 8 | class StorageService { |
@@ -34,9 +37,33 @@ class StorageService { |
34 | 37 |
|
35 | 38 | // Specific getters and setters |
36 | 39 | /// Authorization token for API requests. |
| 40 | + /// |
| 41 | + /// Stored across sessions. |
37 | 42 | static String? get authToken => _get<String>(AppStrings.authTokenKey); |
38 | 43 |
|
39 | 44 | /// Authorization token for API requests. |
| 45 | + /// |
| 46 | + /// Stored across sessions. |
40 | 47 | static set authToken(String? token) => |
41 | 48 | _set<String>(AppStrings.authTokenKey, token); |
| 49 | + |
| 50 | + /// Currently logged in user. |
| 51 | + static User? get user { |
| 52 | + try { |
| 53 | + return User.fromJson(json.decode(_get<String>(AppStrings.userKey)!)); |
| 54 | + } on Exception catch (_) { |
| 55 | + // This just means that the user has not been stored previously. |
| 56 | + return null; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// Currently logged in user. |
| 61 | + static set user(User? u) { |
| 62 | + try { |
| 63 | + _set<String>(AppStrings.userKey, json.encode(u!.toJson())); |
| 64 | + } on Exception catch (_) { |
| 65 | + // This just means that the passed object is null. |
| 66 | + _set<String?>(AppStrings.userKey, null); |
| 67 | + } |
| 68 | + } |
42 | 69 | } |
0 commit comments