♻️ Better http client management, no more expired token
This commit is contained in:
@ -43,7 +43,7 @@ class AccountProvider extends GetxController {
|
||||
}
|
||||
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
auth.ensureCredentials();
|
||||
|
||||
if (auth.credentials == null) await auth.loadCredentials();
|
||||
|
||||
@ -91,11 +91,11 @@ class AccountProvider extends GetxController {
|
||||
},
|
||||
onDone: () {
|
||||
isConnected.value = false;
|
||||
Future.delayed(const Duration(milliseconds: 1000), () => connect());
|
||||
Future.delayed(const Duration(seconds: 3), () => connect());
|
||||
},
|
||||
onError: (err) {
|
||||
isConnected.value = false;
|
||||
Future.delayed(const Duration(milliseconds: 1000), () => connect());
|
||||
Future.delayed(const Duration(seconds: 3), () => connect());
|
||||
},
|
||||
);
|
||||
}
|
||||
@ -153,9 +153,7 @@ class AccountProvider extends GetxController {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) return;
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['passport'];
|
||||
client.httpClient.addAuthenticator(auth.requestAuthenticator);
|
||||
final client = auth.configureClient(service: 'passport');
|
||||
|
||||
final resp = await client.get('/api/notifications?skip=0&take=100');
|
||||
if (resp.statusCode == 200) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
@ -49,17 +50,42 @@ class AuthProvider extends GetConnect {
|
||||
}
|
||||
|
||||
Future<Request<T?>> requestAuthenticator<T>(Request<T?> request) async {
|
||||
if (credentials != null && credentials!.isExpired) {
|
||||
refreshCredentials();
|
||||
}
|
||||
|
||||
if (credentials != null) {
|
||||
try {
|
||||
await ensureCredentials();
|
||||
request.headers['Authorization'] = 'Bearer ${credentials!.accessToken}';
|
||||
}
|
||||
} catch (_) {}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
GetConnect configureClient({
|
||||
String? service,
|
||||
timeout = const Duration(seconds: 5),
|
||||
}) {
|
||||
final client = GetConnect(
|
||||
maxAuthRetries: 3,
|
||||
timeout: timeout,
|
||||
allowAutoSignedCert: true,
|
||||
);
|
||||
client.httpClient.addAuthenticator(requestAuthenticator);
|
||||
|
||||
if (service != null) {
|
||||
client.httpClient.baseUrl = ServiceFinder.services[service];
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
Future<void> ensureCredentials() async {
|
||||
if (!await isAuthorized) throw Exception('unauthorized');
|
||||
if (credentials == null) await loadCredentials();
|
||||
|
||||
if (credentials!.isExpired) {
|
||||
await refreshCredentials();
|
||||
log("Refreshed credentials at ${DateTime.now()}");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadCredentials() async {
|
||||
if (await isAuthorized) {
|
||||
final content = await storage.read(key: 'auth_credentials');
|
||||
@ -124,9 +150,7 @@ class AuthProvider extends GetConnect {
|
||||
return _cachedUserProfileResponse!;
|
||||
}
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['passport'];
|
||||
client.httpClient.addAuthenticator(requestAuthenticator);
|
||||
final client = configureClient(service: 'passport');
|
||||
|
||||
final resp = await client.get('/api/users/me');
|
||||
if (resp.statusCode != 200) {
|
||||
|
@ -24,9 +24,7 @@ class ChatProvider extends GetxController {
|
||||
}
|
||||
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
if (auth.credentials == null) await auth.loadCredentials();
|
||||
auth.ensureCredentials();
|
||||
|
||||
final uri = Uri.parse(
|
||||
'${ServiceFinder.services['messaging']}/api/ws?tk=${auth.credentials!.accessToken}'
|
||||
@ -65,11 +63,11 @@ class ChatProvider extends GetxController {
|
||||
},
|
||||
onDone: () {
|
||||
isConnected.value = false;
|
||||
Future.delayed(const Duration(milliseconds: 1000), () => connect());
|
||||
Future.delayed(const Duration(seconds: 3), () => connect());
|
||||
},
|
||||
onError: (err) {
|
||||
isConnected.value = false;
|
||||
Future.delayed(const Duration(milliseconds: 1000), () => connect());
|
||||
Future.delayed(const Duration(seconds: 3), () => connect());
|
||||
},
|
||||
);
|
||||
}
|
||||
|
@ -50,12 +50,10 @@ class AttachmentProvider extends GetConnect {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
final client = GetConnect(
|
||||
maxAuthRetries: 3,
|
||||
final client = auth.configureClient(
|
||||
service: 'paperclip',
|
||||
timeout: const Duration(minutes: 3),
|
||||
);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['paperclip'];
|
||||
client.httpClient.addAuthenticator(auth.requestAuthenticator);
|
||||
|
||||
final filePayload =
|
||||
MultipartFile(await file.readAsBytes(), filename: basename(file.path));
|
||||
@ -99,9 +97,7 @@ class AttachmentProvider extends GetConnect {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['paperclip'];
|
||||
client.httpClient.addAuthenticator(auth.requestAuthenticator);
|
||||
final client = auth.configureClient(service: 'paperclip');
|
||||
|
||||
var resp = await client.put('/api/attachments/$id', {
|
||||
'metadata': {
|
||||
@ -123,9 +119,7 @@ class AttachmentProvider extends GetConnect {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['paperclip'];
|
||||
client.httpClient.addAuthenticator(auth.requestAuthenticator);
|
||||
final client = auth.configureClient(service: 'paperclip');
|
||||
|
||||
var resp = await client.delete('/api/attachments/$id');
|
||||
if (resp.statusCode != 200) {
|
||||
|
@ -9,7 +9,6 @@ import 'package:solian/models/call.dart';
|
||||
import 'package:solian/models/channel.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/screens/channel/call/call.dart';
|
||||
import 'package:solian/services.dart';
|
||||
import 'package:wakelock_plus/wakelock_plus.dart';
|
||||
|
||||
class ChatCallProvider extends GetxController {
|
||||
@ -64,9 +63,7 @@ class ChatCallProvider extends GetxController {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['messaging'];
|
||||
client.httpClient.addAuthenticator(auth.requestAuthenticator);
|
||||
final client = auth.configureClient(service: 'messaging');
|
||||
|
||||
final resp = await client.post(
|
||||
'/api/channels/global/${channel.value!.alias}/calls/ongoing/token',
|
||||
|
@ -1,7 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/services.dart';
|
||||
import 'package:solian/widgets/account/friend_select.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
@ -10,8 +9,7 @@ class ChannelProvider extends GetxController {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['messaging'];
|
||||
final client = auth.configureClient(service: 'messaging');
|
||||
|
||||
final resp = await client.get('/api/channels/$realm/$alias');
|
||||
if (resp.statusCode != 200) {
|
||||
@ -26,8 +24,7 @@ class ChannelProvider extends GetxController {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['messaging'];
|
||||
final client = auth.configureClient(service: 'messaging');
|
||||
|
||||
final resp = await client.get('/api/channels/$realm/$alias/calls/ongoing');
|
||||
if (resp.statusCode == 404) {
|
||||
@ -43,9 +40,7 @@ class ChannelProvider extends GetxController {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['messaging'];
|
||||
client.httpClient.addAuthenticator(auth.requestAuthenticator);
|
||||
final client = auth.configureClient(service: 'messaging');
|
||||
|
||||
final resp = await client.get('/api/channels/$scope');
|
||||
if (resp.statusCode != 200) {
|
||||
@ -59,9 +54,7 @@ class ChannelProvider extends GetxController {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['messaging'];
|
||||
client.httpClient.addAuthenticator(auth.requestAuthenticator);
|
||||
final client = auth.configureClient(service: 'messaging');
|
||||
|
||||
final resp = await client.get('/api/channels/$realm/me/available');
|
||||
if (resp.statusCode != 200) {
|
||||
@ -75,9 +68,7 @@ class ChannelProvider extends GetxController {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['messaging'];
|
||||
client.httpClient.addAuthenticator(auth.requestAuthenticator);
|
||||
final client = auth.configureClient(service: 'messaging');
|
||||
|
||||
final resp = await client.post('/api/channels/$scope', payload);
|
||||
if (resp.statusCode != 200) {
|
||||
@ -102,9 +93,7 @@ class ChannelProvider extends GetxController {
|
||||
if (related == null) return null;
|
||||
|
||||
final prof = await auth.getProfile();
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['messaging'];
|
||||
client.httpClient.addAuthenticator(auth.requestAuthenticator);
|
||||
final client = auth.configureClient(service: 'messaging');
|
||||
|
||||
final resp = await client.post('/api/channels/$scope/dm', {
|
||||
'alias': const Uuid().v4().replaceAll('-', '').substring(0, 12),
|
||||
@ -125,9 +114,7 @@ class ChannelProvider extends GetxController {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['messaging'];
|
||||
client.httpClient.addAuthenticator(auth.requestAuthenticator);
|
||||
final client = auth.configureClient(service: 'messaging');
|
||||
|
||||
final resp = await client.put('/api/channels/$scope/$id', payload);
|
||||
if (resp.statusCode != 200) {
|
||||
|
@ -1,14 +1,12 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/services.dart';
|
||||
|
||||
class RealmProvider extends GetxController {
|
||||
Future<Response> getRealm(String alias) async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['passport'];
|
||||
final client = auth.configureClient(service: 'passport');
|
||||
|
||||
final resp = await client.get('/api/realms/$alias');
|
||||
if (resp.statusCode != 200) {
|
||||
@ -22,9 +20,7 @@ class RealmProvider extends GetxController {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) throw Exception('unauthorized');
|
||||
|
||||
final client = GetConnect(maxAuthRetries: 3);
|
||||
client.httpClient.baseUrl = ServiceFinder.services['passport'];
|
||||
client.httpClient.addAuthenticator(auth.requestAuthenticator);
|
||||
final client = auth.configureClient(service: 'passport');
|
||||
|
||||
final resp = await client.get('/api/realms/me/available');
|
||||
if (resp.statusCode != 200) {
|
||||
|
Reference in New Issue
Block a user