♻️ Refactored auth system
This commit is contained in:
@ -33,7 +33,7 @@ class StatusProvider extends GetConnect {
|
||||
|
||||
Future<Response> getCurrentStatus() async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('auth');
|
||||
|
||||
@ -53,7 +53,7 @@ class StatusProvider extends GetConnect {
|
||||
DateTime? clearAt,
|
||||
}) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('auth');
|
||||
|
||||
@ -82,7 +82,7 @@ class StatusProvider extends GetConnect {
|
||||
|
||||
Future<Response> clearStatus() async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('auth');
|
||||
|
||||
|
@ -60,7 +60,10 @@ class AuthProvider extends GetConnect {
|
||||
@override
|
||||
void onInit() {
|
||||
httpClient.baseUrl = ServiceFinder.buildUrl('auth', null);
|
||||
loadCredentials();
|
||||
refreshAuthorizeStatus().then((_) {
|
||||
loadCredentials();
|
||||
refreshUserProfile();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> refreshCredentials() async {
|
||||
@ -116,7 +119,7 @@ class AuthProvider extends GetConnect {
|
||||
}
|
||||
|
||||
Future<void> ensureCredentials() async {
|
||||
if (!await isAuthorized) throw Exception('unauthorized');
|
||||
if (isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
if (credentials == null) await loadCredentials();
|
||||
|
||||
if (credentials!.isExpired) {
|
||||
@ -126,7 +129,7 @@ class AuthProvider extends GetConnect {
|
||||
}
|
||||
|
||||
Future<void> loadCredentials() async {
|
||||
if (await isAuthorized) {
|
||||
if (isAuthorized.isTrue) {
|
||||
final content = await storage.read(key: 'auth_credentials');
|
||||
credentials = TokenSet.fromJson(jsonDecode(content!));
|
||||
}
|
||||
@ -137,7 +140,7 @@ class AuthProvider extends GetConnect {
|
||||
String username,
|
||||
String password,
|
||||
) async {
|
||||
_cachedUserProfileResponse = null;
|
||||
userProfile.value = null;
|
||||
|
||||
final client = ServiceFinder.configureClient('auth');
|
||||
|
||||
@ -172,6 +175,8 @@ class AuthProvider extends GetConnect {
|
||||
value: jsonEncode(credentials!.toJson()),
|
||||
);
|
||||
|
||||
await refreshUserProfile();
|
||||
|
||||
Get.find<WebSocketProvider>().connect();
|
||||
Get.find<WebSocketProvider>().notifyPrefetch();
|
||||
|
||||
@ -179,7 +184,8 @@ class AuthProvider extends GetConnect {
|
||||
}
|
||||
|
||||
void signout() {
|
||||
_cachedUserProfileResponse = null;
|
||||
isAuthorized.value = false;
|
||||
userProfile.value = null;
|
||||
|
||||
Get.find<WebSocketProvider>().disconnect();
|
||||
Get.find<WebSocketProvider>().notifications.clear();
|
||||
@ -195,30 +201,21 @@ class AuthProvider extends GetConnect {
|
||||
|
||||
// Data Layer
|
||||
|
||||
Response? _cachedUserProfileResponse;
|
||||
RxBool isAuthorized = false.obs;
|
||||
Rx<Map<String, dynamic>?> userProfile = Rx(null);
|
||||
|
||||
Future<bool> get isAuthorized => storage.containsKey(key: 'auth_credentials');
|
||||
|
||||
Future<Response> getProfile({noCache = false}) async {
|
||||
if (!noCache && _cachedUserProfileResponse != null) {
|
||||
return _cachedUserProfileResponse!;
|
||||
}
|
||||
Future<void> refreshAuthorizeStatus() async {
|
||||
isAuthorized.value = await storage.containsKey(key: 'auth_credentials');
|
||||
}
|
||||
|
||||
Future<void> refreshUserProfile() async {
|
||||
final client = configureClient('auth');
|
||||
|
||||
final resp = await client.get('/users/me');
|
||||
if (resp.statusCode != 200) {
|
||||
throw Exception(resp.bodyString);
|
||||
} else {
|
||||
_cachedUserProfileResponse = resp;
|
||||
}
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
Future<Response?> getProfileWithCheck({noCache = false}) async {
|
||||
if (!await isAuthorized) return null;
|
||||
|
||||
return await getProfile(noCache: noCache);
|
||||
userProfile.value = resp.body;
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ class ChatCallProvider extends GetxController {
|
||||
|
||||
Future<(String, String)> getRoomToken() async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('messaging');
|
||||
|
||||
|
@ -86,7 +86,7 @@ class AttachmentProvider extends GetConnect {
|
||||
Map<String, dynamic>? metadata,
|
||||
) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient(
|
||||
'files',
|
||||
@ -130,7 +130,7 @@ class AttachmentProvider extends GetConnect {
|
||||
bool isMature = false,
|
||||
}) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('files');
|
||||
|
||||
@ -152,7 +152,7 @@ class AttachmentProvider extends GetConnect {
|
||||
|
||||
Future<Response> deleteAttachment(int id) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('files');
|
||||
|
||||
|
@ -16,7 +16,7 @@ class ChannelProvider extends GetxController {
|
||||
|
||||
Future<void> refreshAvailableChannel() async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
isLoading.value = true;
|
||||
final resp = await listAvailableChannel();
|
||||
@ -29,7 +29,7 @@ class ChannelProvider extends GetxController {
|
||||
|
||||
Future<Response> getChannel(String alias, {String realm = 'global'}) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('messaging');
|
||||
|
||||
@ -44,7 +44,7 @@ class ChannelProvider extends GetxController {
|
||||
Future<Response> getMyChannelProfile(String alias,
|
||||
{String realm = 'global'}) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('messaging');
|
||||
|
||||
@ -59,7 +59,7 @@ class ChannelProvider extends GetxController {
|
||||
Future<Response?> getChannelOngoingCall(String alias,
|
||||
{String realm = 'global'}) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('messaging');
|
||||
|
||||
@ -75,7 +75,7 @@ class ChannelProvider extends GetxController {
|
||||
|
||||
Future<Response> listChannel({String scope = 'global'}) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('messaging');
|
||||
|
||||
@ -89,7 +89,7 @@ class ChannelProvider extends GetxController {
|
||||
|
||||
Future<Response> listAvailableChannel({String realm = 'global'}) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('messaging');
|
||||
|
||||
@ -103,7 +103,7 @@ class ChannelProvider extends GetxController {
|
||||
|
||||
Future<Response> createChannel(String scope, dynamic payload) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('messaging');
|
||||
|
||||
@ -118,7 +118,7 @@ class ChannelProvider extends GetxController {
|
||||
Future<Response?> createDirectChannel(
|
||||
BuildContext context, String scope) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final related = await showModalBottomSheet(
|
||||
useRootNavigator: true,
|
||||
@ -129,14 +129,14 @@ class ChannelProvider extends GetxController {
|
||||
);
|
||||
if (related == null) return null;
|
||||
|
||||
final prof = await auth.getProfile();
|
||||
final prof = auth.userProfile.value!;
|
||||
final client = auth.configureClient('messaging');
|
||||
|
||||
final resp = await client.post('/channels/$scope/dm', {
|
||||
'alias': const Uuid().v4().replaceAll('-', '').substring(0, 12),
|
||||
'name': 'DM',
|
||||
'description':
|
||||
'A direct message channel between @${prof.body['name']} and @${related.name}',
|
||||
'A direct message channel between @${prof['name']} and @${related.name}',
|
||||
'related_user': related.id,
|
||||
'is_encrypted': false,
|
||||
});
|
||||
@ -149,7 +149,7 @@ class ChannelProvider extends GetxController {
|
||||
|
||||
Future<Response> updateChannel(String scope, int id, dynamic payload) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('messaging');
|
||||
|
||||
|
@ -27,7 +27,7 @@ class PostProvider extends GetConnect {
|
||||
|
||||
Future<Response> listDraft(int page) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final queries = [
|
||||
'take=${10}',
|
||||
|
@ -4,7 +4,7 @@ import 'package:solian/providers/auth.dart';
|
||||
class RealmProvider extends GetxController {
|
||||
Future<Response> getRealm(String alias) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('auth');
|
||||
|
||||
@ -18,7 +18,7 @@ class RealmProvider extends GetxController {
|
||||
|
||||
Future<Response> listAvailableRealm() async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
if (auth.isAuthorized.isFalse) throw Exception('unauthorized');
|
||||
|
||||
final client = auth.configureClient('auth');
|
||||
|
||||
|
@ -18,7 +18,7 @@ Future<MessageHistoryDb> createHistoryDb() async {
|
||||
|
||||
Future<Event?> getRemoteEvent(int id, Channel channel, String scope) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) return null;
|
||||
if (auth.isAuthorized.isFalse) return null;
|
||||
|
||||
final client = auth.configureClient('messaging');
|
||||
|
||||
@ -48,7 +48,7 @@ Future<(List<Event>, int)?> getRemoteEvents(
|
||||
}
|
||||
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) return null;
|
||||
if (auth.isAuthorized.isFalse) return null;
|
||||
|
||||
final client = auth.configureClient('messaging');
|
||||
|
||||
|
@ -102,7 +102,7 @@ class WebSocketProvider extends GetxController {
|
||||
|
||||
Future<void> notifyPrefetch() async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) return;
|
||||
if (auth.isAuthorized.isFalse) return;
|
||||
|
||||
final client = auth.configureClient('auth');
|
||||
|
||||
@ -119,7 +119,7 @@ class WebSocketProvider extends GetxController {
|
||||
|
||||
Future<void> registerPushNotifications() async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) return;
|
||||
if (auth.isAuthorized.isFalse) return;
|
||||
|
||||
late final String? token;
|
||||
late final String provider;
|
||||
|
Reference in New Issue
Block a user