32 lines
1.0 KiB
Dart
32 lines
1.0 KiB
Dart
import 'package:dio/dio.dart';
|
|
|
|
import '../services/device_header_service.dart';
|
|
import '../storage/secure_identity_storage.dart';
|
|
|
|
final _defaultIdentityStorage = SecureIdentityStorage();
|
|
|
|
Future<void> saveJwt(String token) => _defaultIdentityStorage.saveJwt(token);
|
|
Future<void> clearJwt() => _defaultIdentityStorage.clearJwt();
|
|
Future<String?> loadJwt() => _defaultIdentityStorage.readJwt();
|
|
|
|
class AuthInterceptor extends Interceptor {
|
|
AuthInterceptor(this._identityStorage);
|
|
|
|
final SecureIdentityStorage _identityStorage;
|
|
|
|
@override
|
|
Future<void> onRequest(
|
|
RequestOptions options,
|
|
RequestInterceptorHandler handler,
|
|
) async {
|
|
await DeviceHeaderService.to.ensureInitialized();
|
|
options.headers.addAll(DeviceHeaderService.to.commonHeaders());
|
|
options.headers.addAll(DeviceHeaderService.to.platformIdentityHeaders());
|
|
final jwt = await _identityStorage.readJwt();
|
|
if (jwt != null && jwt.isNotEmpty) {
|
|
options.headers['Authorization'] = 'Bearer $jwt';
|
|
}
|
|
handler.next(options);
|
|
}
|
|
}
|