mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-08 04:07:51 -06:00
Externalized API and models as own package
This commit is contained in:
@@ -1,35 +1,32 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:paperless_mobile/core/model/error_message.dart';
|
||||
import 'package:paperless_mobile/features/login/bloc/authentication_cubit.dart';
|
||||
import 'package:paperless_api/paperless_api.dart';
|
||||
import 'package:paperless_mobile/core/store/local_vault.dart';
|
||||
import 'package:http_interceptor/http_interceptor.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
|
||||
@injectable
|
||||
class AuthenticationInterceptor implements InterceptorContract {
|
||||
AuthenticationCubit authenticationCubit;
|
||||
AuthenticationInterceptor(this.authenticationCubit);
|
||||
final LocalVault _localVault;
|
||||
AuthenticationInterceptor(this._localVault);
|
||||
|
||||
@override
|
||||
Future<BaseRequest> interceptRequest({required BaseRequest request}) async {
|
||||
final authState = authenticationCubit.state;
|
||||
final auth = await _localVault.loadAuthenticationInformation();
|
||||
|
||||
if (kDebugMode) {
|
||||
log("Intercepted ${request.method} request to ${request.url.toString()}");
|
||||
}
|
||||
if (authState.authentication == null) {
|
||||
throw const ErrorMessage(ErrorCode.notAuthenticated);
|
||||
if (auth == null) {
|
||||
throw const PaperlessServerException(ErrorCode.notAuthenticated);
|
||||
}
|
||||
return request.copyWith(
|
||||
//Append server Url
|
||||
url: Uri.parse(
|
||||
authState.authentication!.serverUrl + request.url.toString()),
|
||||
headers: authState.authentication!.token.isEmpty
|
||||
url: Uri.parse(auth.serverUrl + request.url.toString()),
|
||||
headers: auth.token.isEmpty
|
||||
? request.headers
|
||||
: {
|
||||
...request.headers,
|
||||
'Authorization': 'Token ${authState.authentication!.token}'
|
||||
},
|
||||
: {...request.headers, 'Authorization': 'Token ${auth.token}'},
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user