2022-02-03 17:06:44 +01:00
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:flutter/material.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/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-02-03 17:06:44 +01:00
|
|
|
import 'package:immich_mobile/modules/login/models/login_response.model.dart';
|
2022-05-06 14:22:23 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/services/backup.service.dart';
|
2022-02-03 17:06:44 +01:00
|
|
|
import 'package:immich_mobile/shared/services/device_info.service.dart';
|
|
|
|
import 'package:immich_mobile/shared/services/network.service.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/device_info.model.dart';
|
|
|
|
|
|
|
|
class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
|
2022-06-25 20:46:51 +02:00
|
|
|
AuthenticationNotifier(
|
|
|
|
this._deviceInfoService, this._backupService, this._networkService)
|
2022-02-03 17:06:44 +01:00
|
|
|
: super(
|
|
|
|
AuthenticationState(
|
|
|
|
deviceId: "",
|
|
|
|
deviceType: "",
|
|
|
|
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-02-03 17:06:44 +01:00
|
|
|
deviceInfo: DeviceInfoRemote(
|
|
|
|
id: 0,
|
|
|
|
userId: "",
|
|
|
|
deviceId: "",
|
|
|
|
deviceType: "",
|
|
|
|
notificationToken: "",
|
|
|
|
createdAt: "",
|
|
|
|
isAutoBackup: false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
final DeviceInfoService _deviceInfoService;
|
|
|
|
final BackupService _backupService;
|
|
|
|
final NetworkService _networkService;
|
2022-02-03 17:06:44 +01:00
|
|
|
|
2022-06-25 20:46:51 +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-02-05 00:20:23 +01:00
|
|
|
try {
|
|
|
|
bool isServerEndpointVerified = await _networkService.pingServer();
|
|
|
|
if (!isServerEndpointVerified) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (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 {
|
2022-06-25 20:46:51 +02:00
|
|
|
Response res = await _networkService.postRequest(
|
|
|
|
url: 'auth/login', data: {'email': email, 'password': password});
|
2022-02-03 17:06:44 +01:00
|
|
|
|
|
|
|
var payload = LogInReponse.fromJson(res.toString());
|
|
|
|
|
|
|
|
Hive.box(userInfoBox).put(accessTokenKey, payload.accessToken);
|
|
|
|
|
|
|
|
state = state.copyWith(
|
|
|
|
isAuthenticated: true,
|
|
|
|
userId: payload.userId,
|
|
|
|
userEmail: payload.userEmail,
|
2022-05-29 05:35:45 +02:00
|
|
|
firstName: payload.firstName,
|
|
|
|
lastName: payload.lastName,
|
|
|
|
profileImagePath: payload.profileImagePath,
|
|
|
|
isAdmin: payload.isAdmin,
|
2022-06-27 22:13:07 +02:00
|
|
|
shouldChangePassword: payload.shouldChangePassword,
|
2022-02-03 17:06:44 +01:00
|
|
|
);
|
2022-04-25 04:33:10 +02:00
|
|
|
|
|
|
|
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 {
|
2022-06-25 20:46:51 +02:00
|
|
|
Hive.box<HiveSavedLoginInfo>(hiveLoginInfoBox)
|
|
|
|
.delete(savedLoginInfoKey);
|
2022-04-25 04:33:10 +02:00
|
|
|
}
|
2022-02-03 17:06:44 +01:00
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register device info
|
|
|
|
try {
|
2022-06-25 20:46:51 +02:00
|
|
|
Response res = await _networkService.postRequest(
|
2022-06-27 22:13:07 +02:00
|
|
|
url: 'device-info',
|
|
|
|
data: {
|
|
|
|
'deviceId': state.deviceId,
|
|
|
|
'deviceType': state.deviceType,
|
|
|
|
},
|
|
|
|
);
|
2022-02-03 17:06:44 +01:00
|
|
|
|
|
|
|
DeviceInfoRemote deviceInfo = DeviceInfoRemote.fromJson(res.toString());
|
|
|
|
state = state.copyWith(deviceInfo: deviceInfo);
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("ERROR Register Device Info: $e");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> logout() async {
|
|
|
|
Hive.box(userInfoBox).delete(accessTokenKey);
|
|
|
|
state = AuthenticationState(
|
|
|
|
deviceId: "",
|
|
|
|
deviceType: "",
|
|
|
|
userId: "",
|
|
|
|
userEmail: "",
|
2022-05-29 05:35:45 +02:00
|
|
|
firstName: '',
|
|
|
|
lastName: '',
|
|
|
|
profileImagePath: '',
|
2022-06-27 22:13:07 +02:00
|
|
|
shouldChangePassword: false,
|
2022-05-29 05:35:45 +02:00
|
|
|
isAuthenticated: false,
|
|
|
|
isAdmin: false,
|
2022-02-03 17:06:44 +01:00
|
|
|
deviceInfo: DeviceInfoRemote(
|
|
|
|
id: 0,
|
|
|
|
userId: "",
|
|
|
|
deviceId: "",
|
|
|
|
deviceType: "",
|
|
|
|
notificationToken: "",
|
|
|
|
createdAt: "",
|
|
|
|
isAutoBackup: false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
setAutoBackup(bool backupState) async {
|
|
|
|
var deviceInfo = await _deviceInfoService.getDeviceInfo();
|
|
|
|
var deviceId = deviceInfo["deviceId"];
|
|
|
|
var deviceType = deviceInfo["deviceType"];
|
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
DeviceInfoRemote deviceInfoRemote =
|
|
|
|
await _backupService.setAutoBackup(backupState, deviceId, deviceType);
|
2022-02-03 17:06:44 +01:00
|
|
|
state = state.copyWith(deviceInfo: deviceInfoRemote);
|
|
|
|
}
|
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 {
|
|
|
|
Response res = await _networkService.putRequest(
|
|
|
|
url: 'user',
|
|
|
|
data: {
|
|
|
|
'id': state.userId,
|
|
|
|
'password': newPassword,
|
|
|
|
'shouldChangePassword': false,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
state = state.copyWith(shouldChangePassword: false);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
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),
|
|
|
|
ref.watch(networkServiceProvider),
|
|
|
|
);
|
2022-02-03 17:06:44 +01:00
|
|
|
});
|