mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-10 02:07:57 -06:00
Fixed login and error handling
This commit is contained in:
@@ -3,22 +3,19 @@ import 'dart:io';
|
||||
import 'package:hydrated_bloc/hydrated_bloc.dart';
|
||||
import 'package:paperless_api/paperless_api.dart';
|
||||
import 'package:paperless_mobile/core/security/authentication_aware_dio_manager.dart';
|
||||
import 'package:paperless_mobile/core/store/local_vault.dart';
|
||||
import 'package:paperless_mobile/features/login/bloc/authentication_state.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/settings/model/application_settings_state.dart';
|
||||
|
||||
class AuthenticationCubit extends HydratedCubit<AuthenticationState> {
|
||||
class AuthenticationCubit extends Cubit<AuthenticationState>
|
||||
with HydratedMixin<AuthenticationState> {
|
||||
final LocalAuthenticationService _localAuthService;
|
||||
final PaperlessAuthenticationApi _authApi;
|
||||
final LocalVault _localVault;
|
||||
final AuthenticationAwareDioManager _dioWrapper;
|
||||
|
||||
AuthenticationCubit(
|
||||
this._localVault,
|
||||
this._localAuthService,
|
||||
this._authApi,
|
||||
this._dioWrapper,
|
||||
@@ -31,6 +28,7 @@ class AuthenticationCubit extends HydratedCubit<AuthenticationState> {
|
||||
}) async {
|
||||
assert(credentials.username != null && credentials.password != null);
|
||||
try {
|
||||
print(_dioWrapper.client.hashCode);
|
||||
_dioWrapper.updateSettings(
|
||||
baseUrl: serverUrl,
|
||||
clientCertificate: clientCertificate,
|
||||
@@ -41,19 +39,22 @@ class AuthenticationCubit extends HydratedCubit<AuthenticationState> {
|
||||
password: credentials.password!,
|
||||
);
|
||||
|
||||
final auth = AuthenticationInformation(
|
||||
serverUrl: serverUrl,
|
||||
_dioWrapper.updateSettings(
|
||||
baseUrl: serverUrl,
|
||||
clientCertificate: clientCertificate,
|
||||
token: token,
|
||||
authToken: token,
|
||||
);
|
||||
|
||||
await _localVault.storeAuthenticationInformation(auth);
|
||||
|
||||
emit(AuthenticationState(
|
||||
isAuthenticated: true,
|
||||
wasLoginStored: false,
|
||||
authentication: auth,
|
||||
));
|
||||
emit(
|
||||
AuthenticationState(
|
||||
wasLoginStored: false,
|
||||
authentication: AuthenticationInformation(
|
||||
serverUrl: serverUrl,
|
||||
clientCertificate: clientCertificate,
|
||||
token: token,
|
||||
),
|
||||
),
|
||||
);
|
||||
} on TlsException catch (_) {
|
||||
const error = PaperlessServerException(
|
||||
ErrorCode.invalidClientCertificateConfiguration);
|
||||
@@ -67,59 +68,68 @@ class AuthenticationCubit extends HydratedCubit<AuthenticationState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> restoreSessionState() async {
|
||||
final storedAuth = await _localVault.loadAuthenticationInformation();
|
||||
late ApplicationSettingsState? appSettings;
|
||||
try {
|
||||
appSettings = await _localVault.loadApplicationSettings() ??
|
||||
ApplicationSettingsState.defaultSettings;
|
||||
} catch (err) {
|
||||
appSettings = ApplicationSettingsState.defaultSettings;
|
||||
///
|
||||
/// Performs a conditional hydration based on the local authentication success.
|
||||
///
|
||||
Future<void> restoreSessionState(bool promptForLocalAuthentication) async {
|
||||
final json = HydratedBloc.storage.read(storageToken);
|
||||
|
||||
if (json == null) {
|
||||
// If there is nothing to restore, we can quit here.
|
||||
return;
|
||||
}
|
||||
if (storedAuth == null || !storedAuth.isValid) {
|
||||
return emit(
|
||||
AuthenticationState(isAuthenticated: false, wasLoginStored: false),
|
||||
);
|
||||
} else {
|
||||
if (appSettings.isLocalAuthenticationEnabled) {
|
||||
final localAuthSuccess = await _localAuthService
|
||||
.authenticateLocalUser("Authenticate to log back in");
|
||||
if (localAuthSuccess) {
|
||||
|
||||
if (promptForLocalAuthentication) {
|
||||
final localAuthSuccess = await _localAuthService
|
||||
.authenticateLocalUser("Authenticate to log back in");
|
||||
if (localAuthSuccess) {
|
||||
hydrate();
|
||||
if (state.isAuthenticated) {
|
||||
_dioWrapper.updateSettings(
|
||||
clientCertificate: storedAuth.clientCertificate,
|
||||
clientCertificate: state.authentication!.clientCertificate,
|
||||
authToken: state.authentication!.token,
|
||||
baseUrl: state.authentication!.serverUrl,
|
||||
);
|
||||
return emit(
|
||||
AuthenticationState(
|
||||
isAuthenticated: true,
|
||||
wasLoginStored: true,
|
||||
authentication: storedAuth,
|
||||
authentication: state.authentication,
|
||||
wasLocalAuthenticationSuccessful: true,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return emit(AuthenticationState(
|
||||
isAuthenticated: false,
|
||||
wasLoginStored: true,
|
||||
wasLocalAuthenticationSuccessful: false,
|
||||
));
|
||||
}
|
||||
} else {
|
||||
hydrate();
|
||||
return emit(
|
||||
AuthenticationState(
|
||||
wasLoginStored: true,
|
||||
wasLocalAuthenticationSuccessful: false,
|
||||
authentication: state.authentication,
|
||||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
hydrate();
|
||||
if (state.isAuthenticated) {
|
||||
_dioWrapper.updateSettings(
|
||||
clientCertificate: storedAuth.clientCertificate,
|
||||
clientCertificate: state.authentication!.clientCertificate,
|
||||
authToken: state.authentication!.token,
|
||||
baseUrl: state.authentication!.serverUrl,
|
||||
);
|
||||
final authState = AuthenticationState(
|
||||
isAuthenticated: true,
|
||||
authentication: storedAuth,
|
||||
authentication: state.authentication!,
|
||||
wasLoginStored: true,
|
||||
);
|
||||
return emit(authState);
|
||||
} else {
|
||||
return emit(AuthenticationState.initial);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await _localVault.clear();
|
||||
await super.clear();
|
||||
await clear();
|
||||
_dioWrapper.resetSettings();
|
||||
emit(AuthenticationState.initial);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user