2022-02-03 17:06:44 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2022-10-14 23:13:35 +02:00
|
|
|
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';
|
2022-10-16 09:50:31 +02:00
|
|
|
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';
|
2022-04-25 04:33:10 +02:00
|
|
|
import 'package:immich_mobile/modules/login/models/hive_saved_login_info.model.dart';
|
2022-05-06 14:22:23 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/services/backup.service.dart';
|
2022-08-03 07:04:34 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
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';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:openapi/api.dart';
|
2022-02-03 17:06:44 +01:00
|
|
|
|
|
|
|
class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
|
2022-06-25 20:46:51 +02:00
|
|
|
AuthenticationNotifier(
|
2022-07-13 14:23:48 +02:00
|
|
|
this._deviceInfoService,
|
|
|
|
this._backupService,
|
|
|
|
this._apiService,
|
2022-10-16 09:50:31 +02:00
|
|
|
this._assetCacheService,
|
2022-10-17 14:53:27 +02:00
|
|
|
this._albumCacheService,
|
2022-10-18 14:06:35 +02:00
|
|
|
this._sharedAlbumCacheService,
|
2022-07-13 14:23:48 +02:00
|
|
|
) : super(
|
2022-02-03 17:06:44 +01:00
|
|
|
AuthenticationState(
|
|
|
|
deviceId: "",
|
2022-07-13 14:23:48 +02:00
|
|
|
deviceType: DeviceTypeEnum.ANDROID,
|
2022-02-03 17:06:44 +01:00
|
|
|
userId: "",
|
|
|
|
userEmail: "",
|
2022-05-29 05:35:45 +02:00
|
|
|
firstName: '',
|
|
|
|
lastName: '',
|
|
|
|
profileImagePath: '',
|
|
|
|
isAdmin: false,
|
2022-06-27 22:13:07 +02:00
|
|
|
shouldChangePassword: false,
|
2022-05-29 05:35:45 +02:00
|
|
|
isAuthenticated: false,
|
2022-07-13 14:23:48 +02:00
|
|
|
deviceInfo: DeviceInfoResponseDto(
|
2022-02-03 17:06:44 +01:00
|
|
|
id: 0,
|
|
|
|
userId: "",
|
|
|
|
deviceId: "",
|
2022-07-13 14:23:48 +02:00
|
|
|
deviceType: DeviceTypeEnum.ANDROID,
|
2022-02-03 17:06:44 +01:00
|
|
|
createdAt: "",
|
|
|
|
isAutoBackup: false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
final DeviceInfoService _deviceInfoService;
|
|
|
|
final BackupService _backupService;
|
2022-07-13 14:23:48 +02:00
|
|
|
final ApiService _apiService;
|
2022-10-16 09:50:31 +02:00
|
|
|
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;
|
2022-07-13 14:23:48 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
// Check Server URL validity
|
2022-02-05 00:20:23 +01:00
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
_apiService.setEndpoint(Hive.box(userInfoBox).get(serverEndpointKey));
|
|
|
|
await _apiService.serverInfoApi.pingServer();
|
2022-02-05 00:20:23 +01:00
|
|
|
} catch (e) {
|
2022-07-13 14:23:48 +02:00
|
|
|
debugPrint('Invalid Server Endpoint Url $e');
|
2022-02-03 17:06:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sign-in request
|
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
var loginResponse = await _apiService.authenticationApi.login(
|
|
|
|
LoginCredentialDto(
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
),
|
|
|
|
);
|
2022-02-03 17:06:44 +01:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
if (loginResponse == null) {
|
|
|
|
debugPrint('Login Response is null');
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-03 17:06:44 +01:00
|
|
|
|
2022-11-20 18:43:10 +01:00
|
|
|
return setSuccessLoginInfo(
|
|
|
|
accessToken: loginResponse.accessToken,
|
2022-11-21 12:29:43 +01:00
|
|
|
serverUrl: serverEndpoint,
|
2022-11-20 18:43:10 +01:00
|
|
|
isSavedLoginInfo: isSavedLoginInfo,
|
2022-02-03 17:06:44 +01:00
|
|
|
);
|
|
|
|
} catch (e) {
|
2022-10-14 23:13:35 +02:00
|
|
|
HapticFeedback.vibrate();
|
2022-07-13 14:23:48 +02:00
|
|
|
debugPrint("Error logging in $e");
|
2022-02-03 17:06:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> logout() async {
|
2022-07-13 14:23:48 +02:00
|
|
|
state = state.copyWith(isAuthenticated: false);
|
2022-11-28 17:01:09 +01:00
|
|
|
await Future.wait([
|
|
|
|
Hive.box(userInfoBox).delete(accessTokenKey),
|
|
|
|
Hive.box(userInfoBox).delete(assetEtagKey),
|
|
|
|
_assetCacheService.invalidate(),
|
|
|
|
_albumCacheService.invalidate(),
|
|
|
|
_sharedAlbumCacheService.invalidate(),
|
|
|
|
]);
|
2022-10-24 21:45:58 +02:00
|
|
|
|
|
|
|
// Remove login info from local storage
|
|
|
|
var loginInfo =
|
|
|
|
Hive.box<HiveSavedLoginInfo>(hiveLoginInfoBox).get(savedLoginInfoKey);
|
|
|
|
if (loginInfo != null) {
|
|
|
|
loginInfo.email = "";
|
|
|
|
loginInfo.password = "";
|
|
|
|
loginInfo.isSaveLogin = false;
|
|
|
|
|
2022-11-28 17:01:09 +01:00
|
|
|
await Hive.box<HiveSavedLoginInfo>(hiveLoginInfoBox).put(
|
2022-10-24 21:45:58 +02:00
|
|
|
savedLoginInfoKey,
|
|
|
|
loginInfo,
|
|
|
|
);
|
|
|
|
}
|
2022-02-03 17:06:44 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
setAutoBackup(bool backupState) async {
|
|
|
|
var deviceInfo = await _deviceInfoService.getDeviceInfo();
|
|
|
|
var deviceId = deviceInfo["deviceId"];
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
DeviceTypeEnum deviceType = deviceInfo["deviceType"];
|
|
|
|
|
|
|
|
DeviceInfoResponseDto updatedDeviceInfo =
|
2022-06-25 20:46:51 +02:00
|
|
|
await _backupService.setAutoBackup(backupState, deviceId, deviceType);
|
2022-07-13 14:23:48 +02:00
|
|
|
|
|
|
|
state = state.copyWith(deviceInfo: updatedDeviceInfo);
|
2022-02-03 17:06:44 +01:00
|
|
|
}
|
2022-05-29 05:35:45 +02:00
|
|
|
|
|
|
|
updateUserProfileImagePath(String path) {
|
|
|
|
state = state.copyWith(profileImagePath: path);
|
|
|
|
}
|
2022-06-27 22:13:07 +02:00
|
|
|
|
|
|
|
Future<bool> changePassword(String newPassword) async {
|
2022-07-13 14:23:48 +02:00
|
|
|
try {
|
|
|
|
await _apiService.userApi.updateUser(
|
|
|
|
UpdateUserDto(
|
|
|
|
id: state.userId,
|
|
|
|
password: newPassword,
|
|
|
|
shouldChangePassword: false,
|
|
|
|
),
|
|
|
|
);
|
2022-06-27 22:13:07 +02:00
|
|
|
|
|
|
|
state = state.copyWith(shouldChangePassword: false);
|
2022-07-13 14:23:48 +02:00
|
|
|
|
2022-06-27 22:13:07 +02:00
|
|
|
return true;
|
2022-07-13 14:23:48 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error changing password $e");
|
2022-06-27 22:13:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-11-20 18:43:10 +01:00
|
|
|
|
|
|
|
Future<bool> setSuccessLoginInfo({
|
|
|
|
required String accessToken,
|
2022-11-21 12:29:43 +01:00
|
|
|
required String serverUrl,
|
2022-11-20 18:43:10 +01:00
|
|
|
required bool isSavedLoginInfo,
|
|
|
|
}) async {
|
|
|
|
_apiService.setAccessToken(accessToken);
|
|
|
|
var userResponseDto = await _apiService.userApi.getMyUserInfo();
|
|
|
|
|
|
|
|
if (userResponseDto != null) {
|
2022-11-21 12:29:43 +01:00
|
|
|
var userInfoHiveBox = await Hive.openBox(userInfoBox);
|
2022-11-20 18:43:10 +01:00
|
|
|
var deviceInfo = await _deviceInfoService.getDeviceInfo();
|
2022-11-21 12:29:43 +01:00
|
|
|
userInfoHiveBox.put(deviceIdKey, deviceInfo["deviceId"]);
|
|
|
|
userInfoHiveBox.put(accessTokenKey, accessToken);
|
|
|
|
userInfoHiveBox.put(serverEndpointKey, serverUrl);
|
2022-11-20 18:43:10 +01:00
|
|
|
|
|
|
|
state = state.copyWith(
|
|
|
|
isAuthenticated: true,
|
|
|
|
userId: userResponseDto.id,
|
|
|
|
userEmail: userResponseDto.email,
|
|
|
|
firstName: userResponseDto.firstName,
|
|
|
|
lastName: userResponseDto.lastName,
|
|
|
|
profileImagePath: userResponseDto.profileImagePath,
|
|
|
|
isAdmin: userResponseDto.isAdmin,
|
|
|
|
shouldChangePassword: userResponseDto.shouldChangePassword,
|
|
|
|
deviceId: deviceInfo["deviceId"],
|
|
|
|
deviceType: deviceInfo["deviceType"],
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isSavedLoginInfo) {
|
|
|
|
// Save login info to local storage
|
|
|
|
Hive.box<HiveSavedLoginInfo>(hiveLoginInfoBox).put(
|
|
|
|
savedLoginInfoKey,
|
|
|
|
HiveSavedLoginInfo(
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
isSaveLogin: true,
|
2022-11-21 12:29:43 +01:00
|
|
|
serverUrl: serverUrl,
|
2022-11-20 18:43:10 +01:00
|
|
|
accessToken: accessToken,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Hive.box<HiveSavedLoginInfo>(hiveLoginInfoBox)
|
|
|
|
.delete(savedLoginInfoKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register device info
|
|
|
|
try {
|
|
|
|
DeviceInfoResponseDto? deviceInfo =
|
2022-12-08 16:57:07 +01:00
|
|
|
await _apiService.deviceInfoApi.upsertDeviceInfo(
|
|
|
|
UpsertDeviceInfoDto(
|
2022-11-20 18:43:10 +01:00
|
|
|
deviceId: state.deviceId,
|
|
|
|
deviceType: state.deviceType,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (deviceInfo == null) {
|
|
|
|
debugPrint('Device Info Response is null');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = state.copyWith(deviceInfo: deviceInfo);
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("ERROR Register Device Info: $e");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-02-03 17:06:44 +01:00
|
|
|
}
|
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
final authenticationProvider =
|
|
|
|
StateNotifierProvider<AuthenticationNotifier, AuthenticationState>((ref) {
|
|
|
|
return AuthenticationNotifier(
|
|
|
|
ref.watch(deviceInfoServiceProvider),
|
|
|
|
ref.watch(backupServiceProvider),
|
2022-07-13 14:23:48 +02:00
|
|
|
ref.watch(apiServiceProvider),
|
2022-10-16 09:50:31 +02:00
|
|
|
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-06-25 20:46:51 +02:00
|
|
|
);
|
2022-02-03 17:06:44 +01:00
|
|
|
});
|