mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-10-29 17:40:28 +00:00
Transfer repository from Gitlab
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:immich_mobile/shared/models/device_info.model.dart';
|
||||
|
||||
class AuthenticationState {
|
||||
final String deviceId;
|
||||
final String deviceType;
|
||||
final String userId;
|
||||
final String userEmail;
|
||||
final bool isAuthenticated;
|
||||
final DeviceInfoRemote deviceInfo;
|
||||
|
||||
AuthenticationState({
|
||||
required this.deviceId,
|
||||
required this.deviceType,
|
||||
required this.userId,
|
||||
required this.userEmail,
|
||||
required this.isAuthenticated,
|
||||
required this.deviceInfo,
|
||||
});
|
||||
|
||||
AuthenticationState copyWith({
|
||||
String? deviceId,
|
||||
String? deviceType,
|
||||
String? userId,
|
||||
String? userEmail,
|
||||
bool? isAuthenticated,
|
||||
DeviceInfoRemote? deviceInfo,
|
||||
}) {
|
||||
return AuthenticationState(
|
||||
deviceId: deviceId ?? this.deviceId,
|
||||
deviceType: deviceType ?? this.deviceType,
|
||||
userId: userId ?? this.userId,
|
||||
userEmail: userEmail ?? this.userEmail,
|
||||
isAuthenticated: isAuthenticated ?? this.isAuthenticated,
|
||||
deviceInfo: deviceInfo ?? this.deviceInfo,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'AuthenticationState(deviceId: $deviceId, deviceType: $deviceType, userId: $userId, userEmail: $userEmail, isAuthenticated: $isAuthenticated, deviceInfo: $deviceInfo)';
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'deviceId': deviceId,
|
||||
'deviceType': deviceType,
|
||||
'userId': userId,
|
||||
'userEmail': userEmail,
|
||||
'isAuthenticated': isAuthenticated,
|
||||
'deviceInfo': deviceInfo.toMap(),
|
||||
};
|
||||
}
|
||||
|
||||
factory AuthenticationState.fromMap(Map<String, dynamic> map) {
|
||||
return AuthenticationState(
|
||||
deviceId: map['deviceId'] ?? '',
|
||||
deviceType: map['deviceType'] ?? '',
|
||||
userId: map['userId'] ?? '',
|
||||
userEmail: map['userEmail'] ?? '',
|
||||
isAuthenticated: map['isAuthenticated'] ?? false,
|
||||
deviceInfo: DeviceInfoRemote.fromMap(map['deviceInfo']),
|
||||
);
|
||||
}
|
||||
|
||||
String toJson() => json.encode(toMap());
|
||||
|
||||
factory AuthenticationState.fromJson(String source) => AuthenticationState.fromMap(json.decode(source));
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other is AuthenticationState &&
|
||||
other.deviceId == deviceId &&
|
||||
other.deviceType == deviceType &&
|
||||
other.userId == userId &&
|
||||
other.userEmail == userEmail &&
|
||||
other.isAuthenticated == isAuthenticated &&
|
||||
other.deviceInfo == deviceInfo;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return deviceId.hashCode ^
|
||||
deviceType.hashCode ^
|
||||
userId.hashCode ^
|
||||
userEmail.hashCode ^
|
||||
isAuthenticated.hashCode ^
|
||||
deviceInfo.hashCode;
|
||||
}
|
||||
}
|
||||
61
mobile/lib/modules/login/models/login_response.model.dart
Normal file
61
mobile/lib/modules/login/models/login_response.model.dart
Normal file
@@ -0,0 +1,61 @@
|
||||
import 'dart:convert';
|
||||
|
||||
class LogInReponse {
|
||||
final String accessToken;
|
||||
final String userId;
|
||||
final String userEmail;
|
||||
|
||||
LogInReponse({
|
||||
required this.accessToken,
|
||||
required this.userId,
|
||||
required this.userEmail,
|
||||
});
|
||||
|
||||
LogInReponse copyWith({
|
||||
String? accessToken,
|
||||
String? userId,
|
||||
String? userEmail,
|
||||
}) {
|
||||
return LogInReponse(
|
||||
accessToken: accessToken ?? this.accessToken,
|
||||
userId: userId ?? this.userId,
|
||||
userEmail: userEmail ?? this.userEmail,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'accessToken': accessToken,
|
||||
'userId': userId,
|
||||
'userEmail': userEmail,
|
||||
};
|
||||
}
|
||||
|
||||
factory LogInReponse.fromMap(Map<String, dynamic> map) {
|
||||
return LogInReponse(
|
||||
accessToken: map['accessToken'] ?? '',
|
||||
userId: map['userId'] ?? '',
|
||||
userEmail: map['userEmail'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
String toJson() => json.encode(toMap());
|
||||
|
||||
factory LogInReponse.fromJson(String source) => LogInReponse.fromMap(json.decode(source));
|
||||
|
||||
@override
|
||||
String toString() => 'LogInReponse(accessToken: $accessToken, userId: $userId, userEmail: $userEmail)';
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other is LogInReponse &&
|
||||
other.accessToken == accessToken &&
|
||||
other.userId == userId &&
|
||||
other.userEmail == userEmail;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => accessToken.hashCode ^ userId.hashCode ^ userEmail.hashCode;
|
||||
}
|
||||
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();
|
||||
});
|
||||
124
mobile/lib/modules/login/ui/login_form.dart
Normal file
124
mobile/lib/modules/login/ui/login_form.dart
Normal file
@@ -0,0 +1,124 @@
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:google_fonts/google_fonts.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
|
||||
|
||||
class LoginForm extends HookConsumerWidget {
|
||||
const LoginForm({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final usernameController = useTextEditingController(text: 'testuser@email.com');
|
||||
final passwordController = useTextEditingController(text: 'password');
|
||||
final serverEndpointController = useTextEditingController(text: 'http://192.168.1.216');
|
||||
|
||||
return Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 300),
|
||||
child: Wrap(
|
||||
spacing: 32,
|
||||
runSpacing: 32,
|
||||
alignment: WrapAlignment.center,
|
||||
children: [
|
||||
const Image(
|
||||
image: AssetImage('assets/immich-logo-no-outline.png'),
|
||||
width: 128,
|
||||
filterQuality: FilterQuality.high,
|
||||
),
|
||||
Text(
|
||||
'IMMICH',
|
||||
style: GoogleFonts.snowburstOne(
|
||||
textStyle:
|
||||
TextStyle(fontWeight: FontWeight.bold, fontSize: 48, color: Theme.of(context).primaryColor)),
|
||||
),
|
||||
EmailInput(controller: usernameController),
|
||||
PasswordInput(controller: passwordController),
|
||||
ServerEndpointInput(controller: serverEndpointController),
|
||||
LoginButton(
|
||||
emailController: usernameController,
|
||||
passwordController: passwordController,
|
||||
serverEndpointController: serverEndpointController,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ServerEndpointInput extends StatelessWidget {
|
||||
final TextEditingController controller;
|
||||
|
||||
const ServerEndpointInput({Key? key, required this.controller}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return TextFormField(
|
||||
controller: controller,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Server Endpoint URL', border: OutlineInputBorder(), hintText: 'http://your-server-ip:port'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class EmailInput extends StatelessWidget {
|
||||
final TextEditingController controller;
|
||||
|
||||
const EmailInput({Key? key, required this.controller}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return TextFormField(
|
||||
controller: controller,
|
||||
decoration:
|
||||
const InputDecoration(labelText: 'email', border: OutlineInputBorder(), hintText: 'youremail@email.com'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class PasswordInput extends StatelessWidget {
|
||||
final TextEditingController controller;
|
||||
|
||||
const PasswordInput({Key? key, required this.controller}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return TextFormField(
|
||||
obscureText: true,
|
||||
controller: controller,
|
||||
decoration: const InputDecoration(labelText: 'Password', border: OutlineInputBorder(), hintText: 'password'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class LoginButton extends ConsumerWidget {
|
||||
final TextEditingController emailController;
|
||||
final TextEditingController passwordController;
|
||||
final TextEditingController serverEndpointController;
|
||||
|
||||
const LoginButton(
|
||||
{Key? key,
|
||||
required this.emailController,
|
||||
required this.passwordController,
|
||||
required this.serverEndpointController})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return ElevatedButton(
|
||||
onPressed: () async {
|
||||
var isAuthenicated = await ref
|
||||
.read(authenticationProvider.notifier)
|
||||
.login(emailController.text, passwordController.text, serverEndpointController.text);
|
||||
|
||||
if (isAuthenicated) {
|
||||
AutoRouter.of(context).pushNamed("/home-page");
|
||||
} else {
|
||||
debugPrint("BAD LOGIN TRY AGAIN - Show UI Here");
|
||||
}
|
||||
},
|
||||
child: const Text("Login"));
|
||||
}
|
||||
}
|
||||
16
mobile/lib/modules/login/views/login_page.dart
Normal file
16
mobile/lib/modules/login/views/login_page.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
|
||||
import 'package:immich_mobile/modules/login/ui/login_form.dart';
|
||||
|
||||
class LoginPage extends HookConsumerWidget {
|
||||
const LoginPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return const Scaffold(
|
||||
body: LoginForm(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user