2022-02-03 10:06:44 -06:00
|
|
|
import 'package:auto_route/auto_route.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2022-04-24 21:33:10 -05:00
|
|
|
import 'package:hive/hive.dart';
|
2022-02-03 10:06:44 -06:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-04-24 21:33:10 -05:00
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
|
|
|
import 'package:immich_mobile/modules/login/models/hive_saved_login_info.model.dart';
|
2022-06-27 15:13:07 -05:00
|
|
|
import 'package:immich_mobile/routing/router.dart';
|
2022-04-23 21:08:45 -05:00
|
|
|
import 'package:immich_mobile/shared/providers/asset.provider.dart';
|
2022-02-03 10:06:44 -06:00
|
|
|
import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
|
2022-05-06 07:22:23 -05:00
|
|
|
import 'package:immich_mobile/modules/backup/providers/backup.provider.dart';
|
2022-02-04 17:20:23 -06:00
|
|
|
import 'package:immich_mobile/shared/ui/immich_toast.dart';
|
2022-02-03 10:06:44 -06:00
|
|
|
|
|
|
|
class LoginForm extends HookConsumerWidget {
|
|
|
|
const LoginForm({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2022-06-25 15:12:47 -05:00
|
|
|
final usernameController =
|
|
|
|
useTextEditingController.fromValue(TextEditingValue.empty);
|
|
|
|
final passwordController =
|
|
|
|
useTextEditingController.fromValue(TextEditingValue.empty);
|
|
|
|
final serverEndpointController =
|
2022-06-27 15:13:07 -05:00
|
|
|
useTextEditingController(text: 'http://your-server-ip:2283/api');
|
2022-04-24 21:33:10 -05:00
|
|
|
final isSaveLoginInfo = useState<bool>(false);
|
|
|
|
|
|
|
|
useEffect(() {
|
2022-06-25 15:12:47 -05:00
|
|
|
var loginInfo =
|
|
|
|
Hive.box<HiveSavedLoginInfo>(hiveLoginInfoBox).get(savedLoginInfoKey);
|
2022-04-24 21:33:10 -05:00
|
|
|
|
|
|
|
if (loginInfo != null) {
|
|
|
|
usernameController.text = loginInfo.email;
|
|
|
|
passwordController.text = loginInfo.password;
|
|
|
|
serverEndpointController.text = loginInfo.serverUrl;
|
|
|
|
isSaveLoginInfo.value = loginInfo.isSaveLogin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}, []);
|
2022-02-03 10:06:44 -06:00
|
|
|
|
|
|
|
return Center(
|
|
|
|
child: ConstrainedBox(
|
|
|
|
constraints: const BoxConstraints(maxWidth: 300),
|
2022-02-13 15:10:42 -06:00
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Wrap(
|
2022-04-24 21:33:10 -05:00
|
|
|
spacing: 16,
|
|
|
|
runSpacing: 16,
|
2022-02-13 15:10:42 -06:00
|
|
|
alignment: WrapAlignment.center,
|
|
|
|
children: [
|
|
|
|
const Image(
|
|
|
|
image: AssetImage('assets/immich-logo-no-outline.png'),
|
2022-04-24 21:33:10 -05:00
|
|
|
width: 100,
|
2022-02-13 15:10:42 -06:00
|
|
|
filterQuality: FilterQuality.high,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
'IMMICH',
|
2022-04-04 09:08:53 -05:00
|
|
|
style: TextStyle(
|
|
|
|
fontFamily: 'SnowburstOne',
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
fontSize: 48,
|
|
|
|
color: Theme.of(context).primaryColor,
|
|
|
|
),
|
2022-02-13 15:10:42 -06:00
|
|
|
),
|
|
|
|
EmailInput(controller: usernameController),
|
|
|
|
PasswordInput(controller: passwordController),
|
|
|
|
ServerEndpointInput(controller: serverEndpointController),
|
2022-04-24 21:33:10 -05:00
|
|
|
CheckboxListTile(
|
|
|
|
activeColor: Theme.of(context).primaryColor,
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
|
|
|
|
dense: true,
|
|
|
|
side: const BorderSide(color: Colors.grey, width: 1.5),
|
2022-06-25 15:12:47 -05:00
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(5)),
|
2022-04-24 21:33:10 -05:00
|
|
|
enableFeedback: true,
|
|
|
|
title: const Text(
|
2022-06-20 18:10:23 -05:00
|
|
|
"Stay logged in",
|
2022-06-25 15:12:47 -05:00
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 16,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
color: Colors.grey),
|
2022-04-24 21:33:10 -05:00
|
|
|
),
|
|
|
|
value: isSaveLoginInfo.value,
|
|
|
|
onChanged: (switchValue) {
|
|
|
|
if (switchValue != null) {
|
|
|
|
isSaveLoginInfo.value = switchValue;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2022-02-13 15:10:42 -06:00
|
|
|
LoginButton(
|
|
|
|
emailController: usernameController,
|
|
|
|
passwordController: passwordController,
|
|
|
|
serverEndpointController: serverEndpointController,
|
2022-04-24 21:33:10 -05:00
|
|
|
isSavedLoginInfo: isSaveLoginInfo.value,
|
2022-02-13 15:10:42 -06:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-02-03 10:06:44 -06:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ServerEndpointInput extends StatelessWidget {
|
|
|
|
final TextEditingController controller;
|
|
|
|
|
2022-06-25 15:12:47 -05:00
|
|
|
const ServerEndpointInput({Key? key, required this.controller})
|
|
|
|
: super(key: key);
|
2022-02-03 10:06:44 -06:00
|
|
|
|
2022-06-12 05:12:20 +02:00
|
|
|
String? _validateInput(String? url) {
|
2022-07-01 10:08:49 +09:00
|
|
|
|
|
|
|
if (url?.startsWith(RegExp(r'https?://')) == true) {
|
2022-06-27 15:13:07 -05:00
|
|
|
return null;
|
2022-07-01 10:08:49 +09:00
|
|
|
} else {
|
2022-06-25 15:12:47 -05:00
|
|
|
return 'Please specify http:// or https://';
|
2022-06-27 15:13:07 -05:00
|
|
|
}
|
2022-06-12 05:12:20 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:06:44 -06:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return TextFormField(
|
|
|
|
controller: controller,
|
|
|
|
decoration: const InputDecoration(
|
2022-06-27 15:13:07 -05:00
|
|
|
labelText: 'Server Endpoint URL',
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
hintText: 'http://your-server-ip:port',
|
|
|
|
),
|
2022-06-12 05:12:20 +02:00
|
|
|
validator: _validateInput,
|
|
|
|
autovalidateMode: AutovalidateMode.always,
|
2022-02-03 10:06:44 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class EmailInput extends StatelessWidget {
|
|
|
|
final TextEditingController controller;
|
|
|
|
|
|
|
|
const EmailInput({Key? key, required this.controller}) : super(key: key);
|
|
|
|
|
2022-06-12 05:12:20 +02:00
|
|
|
String? _validateInput(String? email) {
|
|
|
|
if (email == null || email == '') return null;
|
|
|
|
if (email.endsWith(' ')) return 'Trailing whitespace';
|
|
|
|
if (email.startsWith(' ')) return 'Leading whitespace';
|
|
|
|
if (email.contains(' ') || !email.contains('@')) return 'Invalid Email';
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:06:44 -06:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return TextFormField(
|
|
|
|
controller: controller,
|
2022-06-25 15:12:47 -05:00
|
|
|
decoration: const InputDecoration(
|
2022-06-27 15:13:07 -05:00
|
|
|
labelText: 'Email',
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
hintText: 'youremail@email.com',
|
|
|
|
),
|
2022-06-12 05:12:20 +02:00
|
|
|
validator: _validateInput,
|
|
|
|
autovalidateMode: AutovalidateMode.always,
|
2022-02-03 10:06:44 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-06-25 15:12:47 -05:00
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: 'Password',
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
hintText: 'password'),
|
2022-02-03 10:06:44 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LoginButton extends ConsumerWidget {
|
|
|
|
final TextEditingController emailController;
|
|
|
|
final TextEditingController passwordController;
|
|
|
|
final TextEditingController serverEndpointController;
|
2022-04-24 21:33:10 -05:00
|
|
|
final bool isSavedLoginInfo;
|
2022-02-03 10:06:44 -06:00
|
|
|
|
2022-04-24 21:33:10 -05:00
|
|
|
const LoginButton({
|
|
|
|
Key? key,
|
|
|
|
required this.emailController,
|
|
|
|
required this.passwordController,
|
|
|
|
required this.serverEndpointController,
|
|
|
|
required this.isSavedLoginInfo,
|
|
|
|
}) : super(key: key);
|
2022-02-03 10:06:44 -06:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
return ElevatedButton(
|
2022-05-29 17:32:30 -05:00
|
|
|
style: ElevatedButton.styleFrom(
|
2022-04-24 21:33:10 -05:00
|
|
|
visualDensity: VisualDensity.standard,
|
2022-05-29 17:32:30 -05:00
|
|
|
primary: Theme.of(context).primaryColor,
|
|
|
|
onPrimary: Colors.grey[50],
|
|
|
|
elevation: 2,
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 25),
|
2022-04-24 21:33:10 -05:00
|
|
|
),
|
2022-02-03 10:06:44 -06:00
|
|
|
onPressed: () async {
|
2022-02-07 23:42:35 -06:00
|
|
|
// This will remove current cache asset state of previous user login.
|
|
|
|
ref.watch(assetProvider.notifier).clearAllAsset();
|
|
|
|
|
2022-04-23 21:08:45 -05:00
|
|
|
var isAuthenticated = await ref
|
2022-06-27 15:13:07 -05:00
|
|
|
.watch(authenticationProvider.notifier)
|
2022-06-25 15:12:47 -05:00
|
|
|
.login(emailController.text, passwordController.text,
|
|
|
|
serverEndpointController.text, isSavedLoginInfo);
|
2022-02-03 10:06:44 -06:00
|
|
|
|
2022-04-23 21:08:45 -05:00
|
|
|
if (isAuthenticated) {
|
2022-02-07 23:42:35 -06:00
|
|
|
// Resume backup (if enable) then navigate
|
2022-06-27 15:13:07 -05:00
|
|
|
|
2022-06-30 13:59:02 -05:00
|
|
|
if (ref.watch(authenticationProvider).shouldChangePassword &&
|
|
|
|
!ref.watch(authenticationProvider).isAdmin) {
|
2022-06-27 15:13:07 -05:00
|
|
|
AutoRouter.of(context).push(const ChangePasswordRoute());
|
|
|
|
} else {
|
|
|
|
ref.watch(backupProvider.notifier).resumeBackup();
|
|
|
|
AutoRouter.of(context).pushNamed("/tab-controller-page");
|
|
|
|
}
|
2022-02-03 10:06:44 -06:00
|
|
|
} else {
|
2022-02-04 17:20:23 -06:00
|
|
|
ImmichToast.show(
|
2022-04-02 12:31:53 -05:00
|
|
|
context: context,
|
2022-06-25 15:12:47 -05:00
|
|
|
msg:
|
|
|
|
"Error logging you in, check server url, email and password!",
|
2022-04-02 12:31:53 -05:00
|
|
|
toastType: ToastType.error,
|
|
|
|
);
|
2022-02-03 10:06:44 -06:00
|
|
|
}
|
|
|
|
},
|
2022-04-24 21:33:10 -05:00
|
|
|
child: const Text(
|
|
|
|
"Login",
|
|
|
|
style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
|
|
|
|
));
|
2022-02-03 10:06:44 -06:00
|
|
|
}
|
|
|
|
}
|