mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
Transfer repository from Gitlab
This commit is contained in:
parent
af2efbdbbd
commit
568cc243f0
177 changed files with 13300 additions and 0 deletions
127
mobile/lib/modules/login/providers/authentication.provider.dart
Normal file
127
mobile/lib/modules/login/providers/authentication.provider.dart
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
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';
|
||||
import 'package:immich_mobile/modules/login/models/login_response.model.dart';
|
||||
import 'package:immich_mobile/shared/services/backup.service.dart';
|
||||
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';
|
||||
import 'package:immich_mobile/utils/dio_http_interceptor.dart';
|
||||
|
||||
class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
|
||||
AuthenticationNotifier()
|
||||
: super(
|
||||
AuthenticationState(
|
||||
deviceId: "",
|
||||
deviceType: "",
|
||||
isAuthenticated: false,
|
||||
userId: "",
|
||||
userEmail: "",
|
||||
deviceInfo: DeviceInfoRemote(
|
||||
id: 0,
|
||||
userId: "",
|
||||
deviceId: "",
|
||||
deviceType: "",
|
||||
notificationToken: "",
|
||||
createdAt: "",
|
||||
isAutoBackup: false,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final DeviceInfoService _deviceInfoService = DeviceInfoService();
|
||||
final BackupService _backupService = BackupService();
|
||||
final NetworkService _networkService = NetworkService();
|
||||
|
||||
Future<bool> login(String email, String password, String serverEndpoint) 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);
|
||||
}
|
||||
|
||||
bool isServerEndpointVerified = await _networkService.pingServer();
|
||||
if (!isServerEndpointVerified) {
|
||||
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 {
|
||||
Response res = await _networkService.postRequest(url: 'auth/login', data: {'email': email, 'password': password});
|
||||
|
||||
var payload = LogInReponse.fromJson(res.toString());
|
||||
|
||||
Hive.box(userInfoBox).put(accessTokenKey, payload.accessToken);
|
||||
|
||||
state = state.copyWith(
|
||||
isAuthenticated: true,
|
||||
userId: payload.userId,
|
||||
userEmail: payload.userEmail,
|
||||
);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Register device info
|
||||
try {
|
||||
Response res = await _networkService
|
||||
.postRequest(url: 'device-info', data: {'deviceId': state.deviceId, 'deviceType': state.deviceType});
|
||||
|
||||
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: "",
|
||||
isAuthenticated: false,
|
||||
userId: "",
|
||||
userEmail: "",
|
||||
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"];
|
||||
|
||||
DeviceInfoRemote deviceInfoRemote = await _backupService.setAutoBackup(backupState, deviceId, deviceType);
|
||||
state = state.copyWith(deviceInfo: deviceInfoRemote);
|
||||
}
|
||||
}
|
||||
|
||||
final authenticationProvider = StateNotifierProvider<AuthenticationNotifier, AuthenticationState>((ref) {
|
||||
return AuthenticationNotifier();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue