mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-08 04:07:51 -06:00
Refactored DI, serialization, added feedback to document download
This commit is contained in:
@@ -8,19 +8,19 @@ import 'package:paperless_mobile/di_initializer.dart';
|
||||
import 'package:paperless_mobile/features/login/model/authentication_information.dart';
|
||||
import 'package:paperless_mobile/features/login/model/client_certificate.dart';
|
||||
import 'package:paperless_mobile/features/login/model/user_credentials.model.dart';
|
||||
import 'package:paperless_mobile/features/login/services/authentication.service.dart';
|
||||
import 'package:paperless_mobile/features/login/services/authentication_service.dart';
|
||||
import 'package:paperless_mobile/features/settings/model/application_settings_state.dart';
|
||||
|
||||
const authenticationKey = "authentication";
|
||||
|
||||
@prod
|
||||
@test
|
||||
@singleton
|
||||
class AuthenticationCubit extends Cubit<AuthenticationState> {
|
||||
final LocalAuthenticationService _localAuthService;
|
||||
final PaperlessAuthenticationApi _authApi;
|
||||
final LocalVault localStore;
|
||||
final LocalVault _localVault;
|
||||
|
||||
AuthenticationCubit(
|
||||
this.localStore,
|
||||
this._localVault,
|
||||
this._localAuthService,
|
||||
this._authApi,
|
||||
) : super(AuthenticationState.initial);
|
||||
@@ -37,33 +37,21 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
|
||||
assert(credentials.username != null && credentials.password != null);
|
||||
try {
|
||||
registerSecurityContext(clientCertificate);
|
||||
emit(
|
||||
AuthenticationState(
|
||||
isAuthenticated: false,
|
||||
wasLoginStored: false,
|
||||
authentication: AuthenticationInformation(
|
||||
username: credentials.username!,
|
||||
password: credentials.password!,
|
||||
serverUrl: serverUrl,
|
||||
token: "",
|
||||
clientCertificate: clientCertificate,
|
||||
),
|
||||
),
|
||||
);
|
||||
final token = await _authApi.login(
|
||||
username: credentials.username!,
|
||||
password: credentials.password!,
|
||||
serverUrl: serverUrl,
|
||||
);
|
||||
final auth = AuthenticationInformation(
|
||||
username: credentials.username!,
|
||||
password: credentials.password!,
|
||||
token: token,
|
||||
// Store information required to make requests
|
||||
final currentAuth = AuthenticationInformation(
|
||||
serverUrl: serverUrl,
|
||||
clientCertificate: clientCertificate,
|
||||
);
|
||||
await _localVault.storeAuthenticationInformation(currentAuth);
|
||||
|
||||
await localStore.storeAuthenticationInformation(auth);
|
||||
final token = await _authApi.login(
|
||||
username: credentials.username!,
|
||||
password: credentials.password!,
|
||||
);
|
||||
|
||||
final auth = currentAuth.copyWith(token: token);
|
||||
|
||||
await _localVault.storeAuthenticationInformation(auth);
|
||||
|
||||
emit(AuthenticationState(
|
||||
isAuthenticated: true,
|
||||
@@ -84,10 +72,10 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
|
||||
}
|
||||
|
||||
Future<void> restoreSessionState() async {
|
||||
final storedAuth = await localStore.loadAuthenticationInformation();
|
||||
final storedAuth = await _localVault.loadAuthenticationInformation();
|
||||
late ApplicationSettingsState? appSettings;
|
||||
try {
|
||||
appSettings = await localStore.loadApplicationSettings() ??
|
||||
appSettings = await _localVault.loadApplicationSettings() ??
|
||||
ApplicationSettingsState.defaultSettings;
|
||||
} catch (err) {
|
||||
appSettings = ApplicationSettingsState.defaultSettings;
|
||||
@@ -95,31 +83,40 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
|
||||
if (storedAuth == null || !storedAuth.isValid) {
|
||||
emit(AuthenticationState(isAuthenticated: false, wasLoginStored: false));
|
||||
} else {
|
||||
if (!appSettings.isLocalAuthenticationEnabled ||
|
||||
await _localAuthService
|
||||
.authenticateLocalUser("Authenticate to log back in")) {
|
||||
registerSecurityContext(storedAuth.clientCertificate);
|
||||
emit(
|
||||
AuthenticationState(
|
||||
isAuthenticated: true,
|
||||
if (appSettings.isLocalAuthenticationEnabled) {
|
||||
final localAuthSuccess = await _localAuthService
|
||||
.authenticateLocalUser("Authenticate to log back in");
|
||||
if (localAuthSuccess) {
|
||||
registerSecurityContext(storedAuth.clientCertificate);
|
||||
return emit(
|
||||
AuthenticationState(
|
||||
isAuthenticated: true,
|
||||
wasLoginStored: true,
|
||||
authentication: storedAuth,
|
||||
wasLocalAuthenticationSuccessful: true,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return emit(AuthenticationState(
|
||||
isAuthenticated: false,
|
||||
wasLoginStored: true,
|
||||
authentication: storedAuth,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
emit(AuthenticationState(isAuthenticated: false, wasLoginStored: true));
|
||||
wasLocalAuthenticationSuccessful: false,
|
||||
));
|
||||
}
|
||||
}
|
||||
emit(AuthenticationState(isAuthenticated: false, wasLoginStored: true));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await localStore.clear();
|
||||
await _localVault.clear();
|
||||
emit(AuthenticationState.initial);
|
||||
}
|
||||
}
|
||||
|
||||
class AuthenticationState {
|
||||
final bool wasLoginStored;
|
||||
final bool? wasLocalAuthenticationSuccessful;
|
||||
final bool isAuthenticated;
|
||||
final AuthenticationInformation? authentication;
|
||||
|
||||
@@ -131,6 +128,7 @@ class AuthenticationState {
|
||||
AuthenticationState({
|
||||
required this.isAuthenticated,
|
||||
required this.wasLoginStored,
|
||||
this.wasLocalAuthenticationSuccessful,
|
||||
this.authentication,
|
||||
});
|
||||
|
||||
@@ -138,11 +136,14 @@ class AuthenticationState {
|
||||
bool? wasLoginStored,
|
||||
bool? isAuthenticated,
|
||||
AuthenticationInformation? authentication,
|
||||
bool? wasLocalAuthenticationSuccessful,
|
||||
}) {
|
||||
return AuthenticationState(
|
||||
isAuthenticated: isAuthenticated ?? this.isAuthenticated,
|
||||
wasLoginStored: wasLoginStored ?? this.wasLoginStored,
|
||||
authentication: authentication ?? this.authentication,
|
||||
wasLocalAuthenticationSuccessful: wasLocalAuthenticationSuccessful ??
|
||||
this.wasLocalAuthenticationSuccessful,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user