203 lines
8.7 KiB
Dart
203 lines
8.7 KiB
Dart
import 'package:fbla_ui/shared/api_logic.dart';
|
|
import 'package:fbla_ui/shared/global_vars.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class SignInPage extends StatefulWidget {
|
|
final void Function(bool) refreshAccount;
|
|
|
|
const SignInPage({super.key, required this.refreshAccount});
|
|
|
|
@override
|
|
State<SignInPage> createState() => _SignInPageState();
|
|
}
|
|
|
|
class _SignInPageState extends State<SignInPage> {
|
|
final _signInKey = GlobalKey<FormState>();
|
|
final TextEditingController _usernameController = TextEditingController();
|
|
final TextEditingController _passwordController = TextEditingController();
|
|
String username = '';
|
|
String password = '';
|
|
bool obscurePassword = true;
|
|
bool rememberMe = false;
|
|
bool _isloading = false;
|
|
String? errorMessage;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Admin Sign In'),
|
|
centerTitle: true,
|
|
),
|
|
body: SingleChildScrollView(
|
|
child: Form(
|
|
key: _signInKey,
|
|
child: Center(
|
|
heightFactor: 1.0,
|
|
child: Container(
|
|
padding: const EdgeInsets.fromLTRB(12, 50, 12, 50),
|
|
height: 475,
|
|
width: 500,
|
|
child: Card(
|
|
child: Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 8, 16, 24),
|
|
child: Column(
|
|
children: [
|
|
const Center(
|
|
child: Text(
|
|
'Admin Sign In',
|
|
style: TextStyle(
|
|
fontSize: 30, fontWeight: FontWeight.bold),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: TextFormField(
|
|
onChanged: (value) {
|
|
username = value.trim();
|
|
},
|
|
controller: _usernameController,
|
|
autocorrect: false,
|
|
decoration: const InputDecoration(
|
|
prefixIcon: Icon(Icons.person_outline),
|
|
labelText: 'Username',
|
|
border: OutlineInputBorder()),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: TextFormField(
|
|
onChanged: (value) {
|
|
password = value.trim();
|
|
},
|
|
onFieldSubmitted: (value) async {
|
|
password = value.trim();
|
|
setState(() {
|
|
errorMessage = null;
|
|
_isloading = true;
|
|
});
|
|
jwt = await signIn(username, password).timeout(
|
|
const Duration(seconds: 20), onTimeout: () {
|
|
_isloading = false;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
width: 300,
|
|
behavior: SnackBarBehavior.floating,
|
|
content:
|
|
Text('Could not Sign in (timeout)!')),
|
|
);
|
|
});
|
|
if (!jwt.contains('Error:')) {
|
|
final SharedPreferences prefs =
|
|
await SharedPreferences.getInstance();
|
|
await prefs.setString('username', username);
|
|
await prefs.setString('password', password);
|
|
await prefs.setBool('rememberMe', rememberMe);
|
|
widget.refreshAccount(true);
|
|
Navigator.of(context).pop();
|
|
} else {
|
|
setState(() {
|
|
errorMessage = 'Invalid Username/Password';
|
|
_isloading = false;
|
|
});
|
|
}
|
|
},
|
|
controller: _passwordController,
|
|
autocorrect: false,
|
|
obscureText: obscurePassword,
|
|
decoration: InputDecoration(
|
|
prefixIcon: const Icon(Icons.fingerprint),
|
|
labelText: 'Password',
|
|
border: const OutlineInputBorder(),
|
|
suffixIcon: Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: IconButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
obscurePassword = !obscurePassword;
|
|
});
|
|
},
|
|
icon: obscurePassword
|
|
? const Icon(Icons.visibility_off)
|
|
: const Icon(Icons.visibility),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
if (errorMessage != null)
|
|
Text(
|
|
errorMessage!,
|
|
style: const TextStyle(color: Colors.red),
|
|
),
|
|
CheckboxListTile(
|
|
value: rememberMe,
|
|
onChanged: (value) async {
|
|
setState(() {
|
|
rememberMe = value!;
|
|
});
|
|
},
|
|
title: const Text('Remember me'),
|
|
),
|
|
ElevatedButton.icon(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor:
|
|
Theme.of(context).colorScheme.primary,
|
|
// padding: const EdgeInsets.only(left: 20.0, right: 20.0, top: 12.0, bottom: 12.0),
|
|
),
|
|
icon: _isloading
|
|
? const SizedBox(
|
|
width: 20,
|
|
height: 20,
|
|
child: CircularProgressIndicator(
|
|
color: Colors.white,
|
|
strokeWidth: 3,
|
|
))
|
|
: const Icon(Icons.done, color: Colors.white),
|
|
label: const Text('Sign In',
|
|
style: TextStyle(color: Colors.white)),
|
|
onPressed: () async {
|
|
setState(() {
|
|
errorMessage = null;
|
|
_isloading = true;
|
|
});
|
|
jwt = await signIn(username, password).timeout(
|
|
const Duration(seconds: 20), onTimeout: () {
|
|
_isloading = false;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
width: 300,
|
|
behavior: SnackBarBehavior.floating,
|
|
content:
|
|
Text('Could not Sign in (timeout)!')),
|
|
);
|
|
});
|
|
if (!jwt.contains('Error:')) {
|
|
final SharedPreferences prefs =
|
|
await SharedPreferences.getInstance();
|
|
await prefs.setString('username', username);
|
|
await prefs.setString('password', password);
|
|
await prefs.setBool('rememberMe', rememberMe);
|
|
widget.refreshAccount(true);
|
|
Navigator.of(context).pop();
|
|
} else {
|
|
setState(() {
|
|
errorMessage = 'Invalid Username/Password';
|
|
_isloading = false;
|
|
});
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|