123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:hive/hive.dart';
- import 'package:hooks_riverpod/hooks_riverpod.dart';
- import 'package:immich_mobile/constants/hive_box.dart';
- import 'package:immich_mobile/modules/album/services/album_cache.service.dart';
- import 'package:immich_mobile/modules/home/services/asset_cache.service.dart';
- 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';
- import 'package:immich_mobile/shared/services/device_info.service.dart';
- import 'package:openapi/api.dart';
- class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
- AuthenticationNotifier(
- this._deviceInfoService,
- this._backupService,
- this._apiService,
- this._assetCacheService,
- this._albumCacheService,
- this._sharedAlbumCacheService,
- ) : super(
- AuthenticationState(
- deviceId: "",
- deviceType: DeviceTypeEnum.ANDROID,
- userId: "",
- userEmail: "",
- firstName: '',
- lastName: '',
- profileImagePath: '',
- isAdmin: false,
- shouldChangePassword: false,
- isAuthenticated: false,
- deviceInfo: DeviceInfoResponseDto(
- id: 0,
- userId: "",
- deviceId: "",
- deviceType: DeviceTypeEnum.ANDROID,
- createdAt: "",
- isAutoBackup: false,
- ),
- ),
- );
- final DeviceInfoService _deviceInfoService;
- final BackupService _backupService;
- final ApiService _apiService;
- final AssetCacheService _assetCacheService;
- final AlbumCacheService _albumCacheService;
- final SharedAlbumCacheService _sharedAlbumCacheService;
- Future<bool> login(
- String email,
- String password,
- String serverEndpoint,
- bool isSavedLoginInfo,
- ) async {
- // 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
- try {
- _apiService.setEndpoint(Hive.box(userInfoBox).get(serverEndpointKey));
- await _apiService.serverInfoApi.pingServer();
- } catch (e) {
- debugPrint('Invalid Server Endpoint Url $e');
- return false;
- }
- // Make sign-in request
- try {
- var loginResponse = await _apiService.authenticationApi.login(
- LoginCredentialDto(
- email: email,
- password: password,
- ),
- );
- if (loginResponse == null) {
- debugPrint('Login Response is null');
- return false;
- }
- return setSuccessLoginInfo(
- accessToken: loginResponse.accessToken,
- isSavedLoginInfo: isSavedLoginInfo,
- );
- } catch (e) {
- HapticFeedback.vibrate();
- debugPrint("Error logging in $e");
- return false;
- }
- }
- Future<bool> logout() async {
- Hive.box(userInfoBox).delete(accessTokenKey);
- state = state.copyWith(isAuthenticated: false);
- _assetCacheService.invalidate();
- _albumCacheService.invalidate();
- _sharedAlbumCacheService.invalidate();
- // Remove login info from local storage
- var loginInfo =
- Hive.box<HiveSavedLoginInfo>(hiveLoginInfoBox).get(savedLoginInfoKey);
- if (loginInfo != null) {
- loginInfo.email = "";
- loginInfo.password = "";
- loginInfo.isSaveLogin = false;
- Hive.box<HiveSavedLoginInfo>(hiveLoginInfoBox).put(
- savedLoginInfoKey,
- loginInfo,
- );
- }
- 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);
- }
- 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;
- }
- }
- Future<bool> setSuccessLoginInfo({
- required String accessToken,
- required bool isSavedLoginInfo,
- }) async {
- Hive.box(userInfoBox).put(accessTokenKey, accessToken);
- _apiService.setAccessToken(accessToken);
- var userResponseDto = await _apiService.userApi.getMyUserInfo();
- if (userResponseDto != null) {
- var deviceInfo = await _deviceInfoService.getDeviceInfo();
- Hive.box(userInfoBox).put(deviceIdKey, deviceInfo["deviceId"]);
- 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,
- serverUrl: Hive.box(userInfoBox).get(serverEndpointKey),
- accessToken: accessToken,
- ),
- );
- } else {
- Hive.box<HiveSavedLoginInfo>(hiveLoginInfoBox)
- .delete(savedLoginInfoKey);
- }
- }
- // Register device info
- try {
- DeviceInfoResponseDto? deviceInfo =
- await _apiService.deviceInfoApi.createDeviceInfo(
- CreateDeviceInfoDto(
- 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;
- }
- }
- final authenticationProvider =
- StateNotifierProvider<AuthenticationNotifier, AuthenticationState>((ref) {
- return AuthenticationNotifier(
- ref.watch(deviceInfoServiceProvider),
- ref.watch(backupServiceProvider),
- ref.watch(apiServiceProvider),
- ref.watch(assetCacheServiceProvider),
- ref.watch(albumCacheServiceProvider),
- ref.watch(sharedAlbumCacheServiceProvider),
- );
- });
|