2019-04-22 17:16:26 +00:00
|
|
|
import 'package:comunic/helpers/account_helper.dart';
|
|
|
|
import 'package:comunic/models/authentication_details.dart';
|
2019-11-02 17:09:34 +00:00
|
|
|
import 'package:comunic/ui/routes/create_account_route.dart';
|
2021-02-18 17:28:57 +00:00
|
|
|
import 'package:comunic/ui/routes/forgot_password_route.dart';
|
2020-04-17 13:25:26 +00:00
|
|
|
import 'package:comunic/ui/widgets/init_widget.dart';
|
2020-05-04 17:05:21 +00:00
|
|
|
import 'package:comunic/ui/widgets/login_scaffold.dart';
|
2019-04-21 09:44:07 +00:00
|
|
|
import 'package:comunic/utils/input_utils.dart';
|
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
2019-04-22 17:16:26 +00:00
|
|
|
import 'package:comunic/utils/ui_utils.dart';
|
2019-04-21 08:34:27 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
/// Login route
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
|
|
|
class LoginRoute extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => _LoginRouteState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LoginRouteState extends State<LoginRoute> {
|
2019-04-21 09:44:07 +00:00
|
|
|
String _currEmail;
|
|
|
|
String _currPassword;
|
2019-04-22 17:16:26 +00:00
|
|
|
AuthResult _authResult;
|
2019-04-23 06:46:50 +00:00
|
|
|
bool _loading = false;
|
2019-04-21 09:44:07 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_currEmail = "";
|
|
|
|
_currPassword = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void _emailChanged(String newEmail) {
|
|
|
|
setState(() {
|
|
|
|
_currEmail = newEmail;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _passwordChanged(String newValue) {
|
|
|
|
setState(() {
|
|
|
|
_currPassword = newValue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Call this whenever the user request to perform login
|
2019-04-22 17:16:26 +00:00
|
|
|
Future<void> _submitForm(BuildContext context) async {
|
2019-04-23 06:46:50 +00:00
|
|
|
setState(() {
|
|
|
|
_loading = true;
|
|
|
|
});
|
|
|
|
|
2019-04-22 17:16:26 +00:00
|
|
|
final loginResult = await AccountHelper().signIn(
|
|
|
|
AuthenticationDetails(email: _currEmail, password: _currPassword));
|
2019-04-21 09:44:07 +00:00
|
|
|
|
2019-04-22 17:16:26 +00:00
|
|
|
if (loginResult == AuthResult.SUCCESS)
|
|
|
|
Navigator.pushReplacement(
|
2020-04-17 13:25:26 +00:00
|
|
|
context, MaterialPageRoute(builder: (b) => InitializeWidget()));
|
2019-04-22 17:16:26 +00:00
|
|
|
else
|
|
|
|
setState(() {
|
|
|
|
_authResult = loginResult;
|
2019-04-23 06:46:50 +00:00
|
|
|
_loading = false;
|
2019-04-22 17:16:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-02 17:09:34 +00:00
|
|
|
void _openCreateAccountPage() {
|
|
|
|
Navigator.of(context)
|
|
|
|
.push(MaterialPageRoute(builder: (c) => CreateAccountRoute()));
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:22:06 +00:00
|
|
|
void _openResetPasswordPage() {
|
|
|
|
Navigator.of(context)
|
2021-02-18 17:28:57 +00:00
|
|
|
.push(MaterialPageRoute(builder: (c) => ForgotPasswordRoute()));
|
2020-05-03 12:22:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 17:09:34 +00:00
|
|
|
/// Build error card
|
2019-04-22 17:16:26 +00:00
|
|
|
Widget _buildErrorCard() {
|
2019-04-23 06:46:50 +00:00
|
|
|
if (_authResult == null) return null;
|
2019-04-22 17:16:26 +00:00
|
|
|
|
|
|
|
//Determine the right message
|
2019-04-23 06:46:50 +00:00
|
|
|
final message = (_authResult == AuthResult.INVALID_CREDENTIALS
|
|
|
|
? tr("Invalid credentials!")
|
|
|
|
: (_authResult == AuthResult.TOO_MANY_ATTEMPTS
|
|
|
|
? tr(
|
|
|
|
"Too many unsuccessfull login attempts! Please try again later...")
|
|
|
|
: tr("A network error occured!")));
|
2019-04-22 17:16:26 +00:00
|
|
|
|
|
|
|
return buildErrorCard(message);
|
2019-04-21 09:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Build login form
|
2019-04-22 17:16:26 +00:00
|
|
|
Widget _buildLoginForm() {
|
2019-04-23 15:36:22 +00:00
|
|
|
// Whether the form can be submitted or not
|
|
|
|
final valid = validateEmail(_currEmail) && _currPassword.length >= 3;
|
|
|
|
|
2019-04-21 09:44:07 +00:00
|
|
|
return SingleChildScrollView(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Column(
|
|
|
|
children: <Widget>[
|
2019-04-22 17:16:26 +00:00
|
|
|
Container(
|
|
|
|
child: _buildErrorCard(),
|
|
|
|
),
|
2019-04-21 09:44:07 +00:00
|
|
|
//Email address
|
|
|
|
TextField(
|
|
|
|
keyboardType: TextInputType.emailAddress,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: tr("Email address"),
|
|
|
|
alignLabelWithHint: true,
|
2019-04-22 17:16:26 +00:00
|
|
|
errorText: _currEmail.length > 0 && !validateEmail(_currEmail)
|
2019-04-21 09:44:07 +00:00
|
|
|
? tr("Invalid email address!")
|
|
|
|
: null),
|
|
|
|
onChanged: _emailChanged,
|
|
|
|
),
|
|
|
|
|
|
|
|
//Password
|
|
|
|
TextField(
|
|
|
|
obscureText: true,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: tr("Password"),
|
|
|
|
alignLabelWithHint: true,
|
|
|
|
),
|
|
|
|
onChanged: _passwordChanged,
|
2019-04-23 15:36:22 +00:00
|
|
|
onSubmitted: valid ? (s) => _submitForm(context) : null,
|
2019-04-21 09:44:07 +00:00
|
|
|
),
|
|
|
|
|
2019-04-23 06:46:50 +00:00
|
|
|
Container(
|
|
|
|
padding: EdgeInsets.all(8.0),
|
2019-04-23 15:36:22 +00:00
|
|
|
child: _loading
|
|
|
|
? CircularProgressIndicator()
|
2021-03-13 14:28:34 +00:00
|
|
|
: ElevatedButton(
|
2019-04-23 15:36:22 +00:00
|
|
|
child: Text(tr("Sign in")),
|
|
|
|
onPressed: valid ? () => _submitForm(context) : null,
|
|
|
|
),
|
2019-11-02 17:09:34 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
InkWell(
|
|
|
|
child: Text(
|
|
|
|
tr("Create an account"),
|
2020-05-04 17:05:21 +00:00
|
|
|
style: TextStyle(color: Colors.blue.shade100),
|
2019-11-02 17:09:34 +00:00
|
|
|
),
|
|
|
|
onTap: () => _openCreateAccountPage(),
|
|
|
|
),
|
2020-05-03 12:22:06 +00:00
|
|
|
|
|
|
|
Container(height: 10),
|
|
|
|
|
|
|
|
InkWell(
|
|
|
|
child: Text(
|
|
|
|
tr("Password forgotten"),
|
2020-05-04 17:05:21 +00:00
|
|
|
style: TextStyle(color: Colors.blue.shade100),
|
2020-05-03 12:22:06 +00:00
|
|
|
),
|
|
|
|
onTap: _openResetPasswordPage,
|
|
|
|
),
|
2019-04-21 09:44:07 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-21 08:34:27 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-05-04 17:05:21 +00:00
|
|
|
return LoginScaffold(child: _buildLoginForm());
|
2019-04-21 08:34:27 +00:00
|
|
|
}
|
2019-04-21 09:44:07 +00:00
|
|
|
}
|