1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-06 11:56:46 +01:00
immich/mobile/lib/modules/login/providers/authentication.provider.dart

217 lines
6.6 KiB
Dart
Raw Normal View History

2022-02-03 17:06:44 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2022-02-03 17:06:44 +01:00
import 'package:hive/hive.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/constants/hive_box.dart';
2022-10-17 14:53:27 +02:00
import 'package:immich_mobile/modules/album/services/album_cache.service.dart';
import 'package:immich_mobile/modules/home/services/asset_cache.service.dart';
2022-02-03 17:06:44 +01:00
import 'package:immich_mobile/modules/login/models/authentication_state.model.dart';
import 'package:immich_mobile/modules/login/models/hive_saved_login_info.model.dart';
import 'package:immich_mobile/modules/backup/services/backup.service.dart';
import 'package:immich_mobile/shared/providers/api.provider.dart';
import 'package:immich_mobile/shared/services/api.service.dart';
2022-02-03 17:06:44 +01:00
import 'package:immich_mobile/shared/services/device_info.service.dart';
import 'package:openapi/api.dart';
2022-02-03 17:06:44 +01:00
class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
AuthenticationNotifier(
this._deviceInfoService,
this._backupService,
this._apiService,
this._assetCacheService,
2022-10-17 14:53:27 +02:00
this._albumCacheService,
2022-10-18 14:06:35 +02:00
this._sharedAlbumCacheService,
) : super(
2022-02-03 17:06:44 +01:00
AuthenticationState(
deviceId: "",
deviceType: DeviceTypeEnum.ANDROID,
2022-02-03 17:06:44 +01:00
userId: "",
userEmail: "",
firstName: '',
lastName: '',
profileImagePath: '',
isAdmin: false,
shouldChangePassword: false,
isAuthenticated: false,
deviceInfo: DeviceInfoResponseDto(
2022-02-03 17:06:44 +01:00
id: 0,
userId: "",
deviceId: "",
deviceType: DeviceTypeEnum.ANDROID,
2022-02-03 17:06:44 +01:00
createdAt: "",
isAutoBackup: false,
),
),
);
final DeviceInfoService _deviceInfoService;
final BackupService _backupService;
final ApiService _apiService;
final AssetCacheService _assetCacheService;
2022-10-17 14:53:27 +02:00
final AlbumCacheService _albumCacheService;
2022-10-18 14:06:35 +02:00
final SharedAlbumCacheService _sharedAlbumCacheService;
Future<bool> login(
String email,
String password,
String serverEndpoint,
bool isSavedLoginInfo,
) async {
2022-02-03 17:06:44 +01:00
// Store server endpoint to Hive and test endpoint
if (serverEndpoint[serverEndpoint.length - 1] == "/") {
var validUrl = serverEndpoint.substring(0, serverEndpoint.length - 1);
Hive.box(userInfoBox).put(serverEndpointKey, validUrl);
} else {
Hive.box(userInfoBox).put(serverEndpointKey, serverEndpoint);
}
// Check Server URL validity
2022-02-05 00:20:23 +01:00
try {
_apiService.setEndpoint(Hive.box(userInfoBox).get(serverEndpointKey));
await _apiService.serverInfoApi.pingServer();
2022-02-05 00:20:23 +01:00
} catch (e) {
debugPrint('Invalid Server Endpoint Url $e');
2022-02-03 17:06:44 +01:00
return false;
}
// Store device id to local storage
var deviceInfo = await _deviceInfoService.getDeviceInfo();
Hive.box(userInfoBox).put(deviceIdKey, deviceInfo["deviceId"]);
state = state.copyWith(
deviceId: deviceInfo["deviceId"],
deviceType: deviceInfo["deviceType"],
);
// Make sign-in request
try {
var loginResponse = await _apiService.authenticationApi.login(
LoginCredentialDto(
email: email,
password: password,
),
);
2022-02-03 17:06:44 +01:00
if (loginResponse == null) {
debugPrint('Login Response is null');
return false;
}
2022-02-03 17:06:44 +01:00
Hive.box(userInfoBox).put(accessTokenKey, loginResponse.accessToken);
2022-02-03 17:06:44 +01:00
state = state.copyWith(
isAuthenticated: true,
userId: loginResponse.userId,
userEmail: loginResponse.userEmail,
firstName: loginResponse.firstName,
lastName: loginResponse.lastName,
profileImagePath: loginResponse.profileImagePath,
isAdmin: loginResponse.isAdmin,
shouldChangePassword: loginResponse.shouldChangePassword,
2022-02-03 17:06:44 +01:00
);
// Login Success - Set Access Token to API Client
_apiService.setAccessToken(loginResponse.accessToken);
if (isSavedLoginInfo) {
// Save login info to local storage
Hive.box<HiveSavedLoginInfo>(hiveLoginInfoBox).put(
savedLoginInfoKey,
HiveSavedLoginInfo(
email: email,
password: password,
isSaveLogin: true,
serverUrl: Hive.box(userInfoBox).get(serverEndpointKey),
),
);
} else {
Hive.box<HiveSavedLoginInfo>(hiveLoginInfoBox)
.delete(savedLoginInfoKey);
}
2022-02-03 17:06:44 +01:00
} catch (e) {
HapticFeedback.vibrate();
debugPrint("Error logging in $e");
2022-02-03 17:06:44 +01:00
return false;
}
// Register device info
try {
DeviceInfoResponseDto? deviceInfo =
await _apiService.deviceInfoApi.createDeviceInfo(
CreateDeviceInfoDto(
deviceId: state.deviceId,
deviceType: state.deviceType,
),
);
2022-02-03 17:06:44 +01:00
if (deviceInfo == null) {
debugPrint('Device Info Response is null');
return false;
}
2022-02-03 17:06:44 +01:00
state = state.copyWith(deviceInfo: deviceInfo);
} catch (e) {
debugPrint("ERROR Register Device Info: $e");
return false;
2022-02-03 17:06:44 +01:00
}
return true;
}
Future<bool> logout() async {
Hive.box(userInfoBox).delete(accessTokenKey);
state = state.copyWith(isAuthenticated: false);
_assetCacheService.invalidate();
2022-10-17 14:53:27 +02:00
_albumCacheService.invalidate();
2022-10-18 14:06:35 +02:00
_sharedAlbumCacheService.invalidate();
2022-02-03 17:06:44 +01:00
return true;
}
setAutoBackup(bool backupState) async {
var deviceInfo = await _deviceInfoService.getDeviceInfo();
var deviceId = deviceInfo["deviceId"];
DeviceTypeEnum deviceType = deviceInfo["deviceType"];
DeviceInfoResponseDto updatedDeviceInfo =
await _backupService.setAutoBackup(backupState, deviceId, deviceType);
state = state.copyWith(deviceInfo: updatedDeviceInfo);
2022-02-03 17:06:44 +01:00
}
updateUserProfileImagePath(String path) {
state = state.copyWith(profileImagePath: path);
}
Future<bool> changePassword(String newPassword) async {
try {
await _apiService.userApi.updateUser(
UpdateUserDto(
id: state.userId,
password: newPassword,
shouldChangePassword: false,
),
);
state = state.copyWith(shouldChangePassword: false);
return true;
} catch (e) {
debugPrint("Error changing password $e");
return false;
}
}
2022-02-03 17:06:44 +01:00
}
final authenticationProvider =
StateNotifierProvider<AuthenticationNotifier, AuthenticationState>((ref) {
return AuthenticationNotifier(
ref.watch(deviceInfoServiceProvider),
ref.watch(backupServiceProvider),
ref.watch(apiServiceProvider),
ref.watch(assetCacheServiceProvider),
2022-10-17 14:53:27 +02:00
ref.watch(albumCacheServiceProvider),
2022-10-18 14:06:35 +02:00
ref.watch(sharedAlbumCacheServiceProvider),
);
2022-02-03 17:06:44 +01:00
});