mirror of
https://github.com/immich-app/immich.git
synced 2025-01-07 00:50:23 -05:00
refactor: authentication provider always try network calls and only fail if 401 or no local user
This commit is contained in:
parent
41aefffe09
commit
d3aacbe74b
2 changed files with 53 additions and 58 deletions
|
@ -24,19 +24,17 @@ class SplashScreenPage extends HookConsumerWidget {
|
||||||
final log = Logger("SplashScreenPage");
|
final log = Logger("SplashScreenPage");
|
||||||
|
|
||||||
void performLoggingIn() async {
|
void performLoggingIn() async {
|
||||||
bool isSuccess = false;
|
bool isAuthSuccess = false;
|
||||||
bool deviceIsOffline = false;
|
|
||||||
|
|
||||||
if (accessToken != null && serverUrl != null && endpoint != null) {
|
if (accessToken != null && serverUrl != null && endpoint != null) {
|
||||||
apiService.setEndpoint(endpoint);
|
apiService.setEndpoint(endpoint);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
isSuccess = await ref
|
isAuthSuccess = await ref
|
||||||
.read(authenticationProvider.notifier)
|
.read(authenticationProvider.notifier)
|
||||||
.setSuccessLoginInfo(
|
.setSuccessLoginInfo(
|
||||||
accessToken: accessToken,
|
accessToken: accessToken,
|
||||||
serverUrl: serverUrl,
|
serverUrl: serverUrl,
|
||||||
offlineLogin: deviceIsOffline,
|
|
||||||
);
|
);
|
||||||
} catch (error, stackTrace) {
|
} catch (error, stackTrace) {
|
||||||
log.severe(
|
log.severe(
|
||||||
|
@ -46,14 +44,13 @@ class SplashScreenPage extends HookConsumerWidget {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
isAuthSuccess = false;
|
||||||
log.severe(
|
log.severe(
|
||||||
'Missing authentication and server information from the local storage',
|
'Missing authentication, server, or endpoint info from the local store',
|
||||||
);
|
);
|
||||||
|
|
||||||
isSuccess = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isSuccess) {
|
if (!isAuthSuccess) {
|
||||||
log.severe(
|
log.severe(
|
||||||
'Unable to login using offline or online methods - Logging out completely',
|
'Unable to login using offline or online methods - Logging out completely',
|
||||||
);
|
);
|
||||||
|
|
|
@ -156,7 +156,6 @@ class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
|
||||||
Future<bool> setSuccessLoginInfo({
|
Future<bool> setSuccessLoginInfo({
|
||||||
required String accessToken,
|
required String accessToken,
|
||||||
required String serverUrl,
|
required String serverUrl,
|
||||||
bool offlineLogin = false,
|
|
||||||
}) async {
|
}) async {
|
||||||
_apiService.setAccessToken(accessToken);
|
_apiService.setAccessToken(accessToken);
|
||||||
|
|
||||||
|
@ -165,57 +164,56 @@ class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
|
||||||
Store.tryGet(StoreKey.deviceId) ?? await FlutterUdid.consistentUdid;
|
Store.tryGet(StoreKey.deviceId) ?? await FlutterUdid.consistentUdid;
|
||||||
|
|
||||||
bool shouldChangePassword = false;
|
bool shouldChangePassword = false;
|
||||||
User? user;
|
User? user = Store.tryGet(StoreKey.currentUser);
|
||||||
|
|
||||||
bool retResult = false;
|
UserAdminResponseDto? userResponse;
|
||||||
User? offlineUser = Store.tryGet(StoreKey.currentUser);
|
UserPreferencesResponseDto? userPreferences;
|
||||||
|
try {
|
||||||
// If the user is offline and there is a user saved on the device,
|
var responses = await Future.wait([
|
||||||
// if not try an online login
|
_apiService.usersApi.getMyUser(),
|
||||||
if (offlineLogin && offlineUser != null) {
|
_apiService.usersApi.getMyPreferences(),
|
||||||
user = offlineUser;
|
]);
|
||||||
retResult = false;
|
userResponse = responses[0] as UserAdminResponseDto;
|
||||||
} else {
|
userPreferences = responses[1] as UserPreferencesResponseDto;
|
||||||
UserAdminResponseDto? userResponseDto;
|
} on ApiException catch (error, stackTrace) {
|
||||||
UserPreferencesResponseDto? userPreferences;
|
if (error.code == 401) {
|
||||||
try {
|
_log.severe("Unauthorized access, token likely expired. Logging out.");
|
||||||
userResponseDto = await _apiService.usersApi.getMyUser();
|
|
||||||
userPreferences = await _apiService.usersApi.getMyPreferences();
|
|
||||||
} on ApiException catch (error, stackTrace) {
|
|
||||||
_log.severe(
|
|
||||||
"Error getting user information from the server [API EXCEPTION]",
|
|
||||||
error,
|
|
||||||
stackTrace,
|
|
||||||
);
|
|
||||||
if (error.innerException is SocketException) {
|
|
||||||
state = state.copyWith(isAuthenticated: true);
|
|
||||||
}
|
|
||||||
} catch (error, stackTrace) {
|
|
||||||
_log.severe(
|
|
||||||
"Error getting user information from the server [CATCH ALL]",
|
|
||||||
error,
|
|
||||||
stackTrace,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (userResponseDto != null) {
|
|
||||||
Store.put(StoreKey.deviceId, deviceId);
|
|
||||||
Store.put(StoreKey.deviceIdHash, fastHash(deviceId));
|
|
||||||
Store.put(
|
|
||||||
StoreKey.currentUser,
|
|
||||||
User.fromUserDto(userResponseDto, userPreferences),
|
|
||||||
);
|
|
||||||
Store.put(StoreKey.serverUrl, serverUrl);
|
|
||||||
Store.put(StoreKey.accessToken, accessToken);
|
|
||||||
|
|
||||||
shouldChangePassword = userResponseDto.shouldChangePassword;
|
|
||||||
user = User.fromUserDto(userResponseDto, userPreferences);
|
|
||||||
|
|
||||||
retResult = true;
|
|
||||||
} else {
|
|
||||||
_log.severe("Unable to get user information from the server.");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
_log.severe(
|
||||||
|
"Error getting user information from the server [API EXCEPTION]",
|
||||||
|
stackTrace,
|
||||||
|
);
|
||||||
|
} catch (error, stackTrace) {
|
||||||
|
_log.severe(
|
||||||
|
"Error getting user information from the server [CATCH ALL]",
|
||||||
|
error,
|
||||||
|
stackTrace,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the user information is successfully retrieved, update the store
|
||||||
|
// Due to the flow of the code, this will always happen on first login
|
||||||
|
if (userResponse != null) {
|
||||||
|
Store.put(StoreKey.deviceId, deviceId);
|
||||||
|
Store.put(StoreKey.deviceIdHash, fastHash(deviceId));
|
||||||
|
Store.put(
|
||||||
|
StoreKey.currentUser,
|
||||||
|
User.fromUserDto(userResponse, userPreferences),
|
||||||
|
);
|
||||||
|
Store.put(StoreKey.serverUrl, serverUrl);
|
||||||
|
Store.put(StoreKey.accessToken, accessToken);
|
||||||
|
|
||||||
|
shouldChangePassword = userResponse.shouldChangePassword;
|
||||||
|
user = User.fromUserDto(userResponse, userPreferences);
|
||||||
|
} else {
|
||||||
|
_log.severe("Unable to get user information from the server.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the user is null, the login was not successful
|
||||||
|
// and we don't have a local copy of the user from a prior successful login
|
||||||
|
if (user == null) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
state = state.copyWith(
|
state = state.copyWith(
|
||||||
|
@ -229,7 +227,7 @@ class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
|
||||||
deviceId: deviceId,
|
deviceId: deviceId,
|
||||||
);
|
);
|
||||||
|
|
||||||
return retResult;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue