2022-02-03 10:06:44 -06:00
|
|
|
import 'package:auto_route/auto_route.dart';
|
2022-07-07 20:40:54 +02:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2022-02-03 10:06:44 -06:00
|
|
|
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-07-13 07:23:48 -05:00
|
|
|
useTextEditingController(text: 'login_form_endpoint_hint'.tr());
|
2022-04-24 21:33:10 -05:00
|
|
|
final isSaveLoginInfo = useState<bool>(false);
|
|
|
|
|
2022-07-13 07:23:48 -05:00
|
|
|
useEffect(
|
|
|
|
() {
|
|
|
|
var loginInfo = Hive.box<HiveSavedLoginInfo>(hiveLoginInfoBox)
|
|
|
|
.get(savedLoginInfoKey);
|
|
|
|
|
|
|
|
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(
|
2022-07-13 07:23:48 -05:00
|
|
|
borderRadius: BorderRadius.circular(5),
|
|
|
|
),
|
2022-04-24 21:33:10 -05:00
|
|
|
enableFeedback: true,
|
|
|
|
title: const Text(
|
2022-07-07 20:40:54 +02:00
|
|
|
"login_form_save_login",
|
2022-06-25 15:12:47 -05:00
|
|
|
style: TextStyle(
|
2022-07-13 07:23:48 -05:00
|
|
|
fontSize: 16,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
color: Colors.grey,
|
|
|
|
),
|
2022-07-07 20:40:54 +02:00
|
|
|
).tr(),
|
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-07-07 20:40:54 +02:00
|
|
|
return 'login_form_err_http'.tr();
|
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,
|
2022-07-07 20:40:54 +02:00
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: 'login_form_endpoint_url'.tr(),
|
2022-07-13 07:23:48 -05:00
|
|
|
border: const OutlineInputBorder(),
|
2022-07-07 20:40:54 +02:00
|
|
|
hintText: 'login_form_endpoint_hint'.tr(),
|
2022-06-27 15:13:07 -05:00
|
|
|
),
|
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;
|
2022-07-07 20:40:54 +02:00
|
|
|
if (email.endsWith(' ')) return 'login_form_err_trailing_whitespace'.tr();
|
|
|
|
if (email.startsWith(' ')) return 'login_form_err_leading_whitespace'.tr();
|
2022-07-13 07:23:48 -05:00
|
|
|
if (email.contains(' ') || !email.contains('@')) {
|
2022-07-07 20:40:54 +02:00
|
|
|
return 'login_form_err_invalid_email'.tr();
|
2022-07-13 07:23:48 -05:00
|
|
|
}
|
2022-06-12 05:12:20 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:06:44 -06:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return TextFormField(
|
|
|
|
controller: controller,
|
2022-07-07 20:40:54 +02:00
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: 'login_form_label_email'.tr(),
|
2022-07-13 07:23:48 -05:00
|
|
|
border: const OutlineInputBorder(),
|
2022-07-07 20:40:54 +02:00
|
|
|
hintText: 'login_form_email_hint'.tr(),
|
2022-06-27 15:13:07 -05:00
|
|
|
),
|
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-07-07 20:40:54 +02:00
|
|
|
decoration: InputDecoration(
|
2022-07-13 07:23:48 -05:00
|
|
|
labelText: 'login_form_label_password'.tr(),
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
hintText: 'login_form_password_hint'.tr(),
|
|
|
|
),
|
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-07-13 07:23:48 -05:00
|
|
|
style: ElevatedButton.styleFrom(
|
|
|
|
visualDensity: VisualDensity.standard,
|
2022-10-14 11:15:19 -05:00
|
|
|
backgroundColor: Theme.of(context).primaryColor,
|
|
|
|
foregroundColor: Colors.grey[50],
|
2022-07-13 07:23:48 -05:00
|
|
|
elevation: 2,
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 25),
|
|
|
|
),
|
|
|
|
onPressed: () async {
|
|
|
|
// This will remove current cache asset state of previous user login.
|
|
|
|
ref.watch(assetProvider.notifier).clearAllAsset();
|
|
|
|
|
|
|
|
var isAuthenticated =
|
|
|
|
await ref.watch(authenticationProvider.notifier).login(
|
|
|
|
emailController.text,
|
|
|
|
passwordController.text,
|
|
|
|
serverEndpointController.text,
|
|
|
|
isSavedLoginInfo,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isAuthenticated) {
|
|
|
|
// Resume backup (if enable) then navigate
|
|
|
|
|
|
|
|
if (ref.watch(authenticationProvider).shouldChangePassword &&
|
|
|
|
!ref.watch(authenticationProvider).isAdmin) {
|
|
|
|
AutoRouter.of(context).push(const ChangePasswordRoute());
|
2022-02-03 10:06:44 -06:00
|
|
|
} else {
|
2022-07-13 07:23:48 -05:00
|
|
|
ref.watch(backupProvider.notifier).resumeBackup();
|
|
|
|
AutoRouter.of(context).pushNamed("/tab-controller-page");
|
2022-02-03 10:06:44 -06:00
|
|
|
}
|
2022-07-13 07:23:48 -05:00
|
|
|
} else {
|
|
|
|
ImmichToast.show(
|
|
|
|
context: context,
|
|
|
|
msg: "login_form_failed_login".tr(),
|
|
|
|
toastType: ToastType.error,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: const Text(
|
|
|
|
"login_form_button_text",
|
|
|
|
style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
|
|
|
|
).tr(),
|
|
|
|
);
|
2022-02-03 10:06:44 -06:00
|
|
|
}
|
|
|
|
}
|