Better form and remove squares
All checks were successful
ci/woodpecker/push/build Pipeline was successful
All checks were successful
ci/woodpecker/push/build Pipeline was successful
Signed-off-by: Nikolai Rodionov <allanger@posteo.de>
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:softplayer_web/features/authorization/application/authorization_application.dart';
|
||||
import 'package:softplayer_web/features/authorization/application/sign_in_data.dart';
|
||||
import 'package:toastification/toastification.dart';
|
||||
|
||||
class LoginForm extends ConsumerStatefulWidget {
|
||||
const LoginForm({super.key});
|
||||
const LoginForm({super.key, required this.toggleAuth});
|
||||
|
||||
final VoidCallback toggleAuth;
|
||||
@override
|
||||
ConsumerState<LoginForm> createState() => _LoginForm();
|
||||
}
|
||||
@@ -16,21 +20,36 @@ class _LoginForm extends ConsumerState<LoginForm> {
|
||||
final TextEditingController emailCtrl = TextEditingController();
|
||||
final TextEditingController passwordCtrl = TextEditingController();
|
||||
|
||||
void _submitForm() {
|
||||
Future<void> _submitForm() async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
// If valid, you can use the values
|
||||
final email = emailCtrl.text;
|
||||
final password = passwordCtrl.text;
|
||||
final form = SignInData(email: email, password: password);
|
||||
ref.read(authorizationControllerProvider.notifier).signin(form);
|
||||
try {
|
||||
await ref.read(authorizationControllerProvider.notifier).signin(form);
|
||||
} catch (e) {
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
log(e.toString());
|
||||
toastification.show(
|
||||
context: context,
|
||||
type: ToastificationType.error,
|
||||
style: ToastificationStyle.flatColored,
|
||||
alignment: Alignment.topRight,
|
||||
autoCloseDuration: const Duration(seconds: 4),
|
||||
title: const Text('Authentication failed'),
|
||||
description: Text(e.toString()),
|
||||
showProgressBar: false,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final state = ref.watch(authorizationControllerProvider);
|
||||
final controller = ref.read(authorizationControllerProvider.notifier);
|
||||
final isLoading = state.isLoading;
|
||||
return SizedBox(
|
||||
width: 400,
|
||||
child: Form(
|
||||
@@ -54,7 +73,7 @@ class _LoginForm extends ConsumerState<LoginForm> {
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
TextButton(
|
||||
onPressed: controller.toggleAuthMode,
|
||||
onPressed: widget.toggleAuth,
|
||||
style: TextButton.styleFrom(
|
||||
padding: EdgeInsets.zero,
|
||||
minimumSize: Size(0, 0),
|
||||
@@ -72,48 +91,26 @@ class _LoginForm extends ConsumerState<LoginForm> {
|
||||
TextFormField(
|
||||
onFieldSubmitted: (_) => _submitForm(),
|
||||
controller: emailCtrl,
|
||||
decoration: InputDecoration(hintText: "Email address"),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Email is required';
|
||||
}
|
||||
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
|
||||
if (!emailRegex.hasMatch(value.trim())) {
|
||||
return 'Enter a valid email address';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(hintText: "Email address"),
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
TextFormField(
|
||||
onFieldSubmitted: (_) => _submitForm(),
|
||||
controller: passwordCtrl,
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(hintText: "Password"),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Password is required';
|
||||
}
|
||||
|
||||
if (value.length < 12) {
|
||||
return 'Password must be at least 12 characters long';
|
||||
}
|
||||
|
||||
final hasNumber = RegExp(r'\d');
|
||||
final hasSpecialChar = RegExp(
|
||||
r'[!@#$%^&*(),.?":{}|<>_\-\\[\]\\/+=~`]',
|
||||
);
|
||||
|
||||
if (!hasNumber.hasMatch(value)) {
|
||||
return 'Password must contain at least one number';
|
||||
}
|
||||
|
||||
if (!hasSpecialChar.hasMatch(value)) {
|
||||
return 'Password must contain at least one special character';
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(hintText: "Password"),
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
SizedBox(
|
||||
@@ -121,13 +118,6 @@ class _LoginForm extends ConsumerState<LoginForm> {
|
||||
child: Text("Forgot password?", textAlign: TextAlign.left),
|
||||
),
|
||||
ElevatedButton(onPressed: _submitForm, child: const Text('Log in')),
|
||||
if (isLoading)
|
||||
Positioned.fill(
|
||||
child: Container(
|
||||
color: Colors.black.withValues(alpha: 0.2),
|
||||
child: const Center(child: CircularProgressIndicator()),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user