mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 16:25:17 +00:00
Can sign in user
This commit is contained in:
12
lib/ui/routes/home_route.dart
Normal file
12
lib/ui/routes/home_route.dart
Normal file
@ -0,0 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Main route of the application
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
class HomeRoute extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Text("Home route");
|
||||
}
|
||||
}
|
123
lib/ui/routes/login_route.dart
Normal file
123
lib/ui/routes/login_route.dart
Normal file
@ -0,0 +1,123 @@
|
||||
import 'package:comunic/helpers/account_helper.dart';
|
||||
import 'package:comunic/models/authentication_details.dart';
|
||||
import 'package:comunic/ui/routes/home_route.dart';
|
||||
import 'package:comunic/utils/input_utils.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:comunic/utils/ui_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Login route
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
class LoginRoute extends StatefulWidget {
|
||||
@override
|
||||
State<StatefulWidget> createState() => _LoginRouteState();
|
||||
}
|
||||
|
||||
class _LoginRouteState extends State<LoginRoute> {
|
||||
String _currEmail;
|
||||
String _currPassword;
|
||||
AuthResult _authResult;
|
||||
|
||||
@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
|
||||
Future<void> _submitForm(BuildContext context) async {
|
||||
final loginResult = await AccountHelper().signIn(
|
||||
AuthenticationDetails(email: _currEmail, password: _currPassword));
|
||||
|
||||
if (loginResult == AuthResult.SUCCESS)
|
||||
Navigator.pushReplacement(
|
||||
context, MaterialPageRoute(builder: (b) => HomeRoute()));
|
||||
else
|
||||
setState(() {
|
||||
_authResult = loginResult;
|
||||
});
|
||||
}
|
||||
|
||||
// Build error card
|
||||
Widget _buildErrorCard() {
|
||||
if (_authResult == null)
|
||||
return null;
|
||||
|
||||
//Determine the right message
|
||||
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!")));
|
||||
|
||||
return buildErrorCard(message);
|
||||
}
|
||||
|
||||
/// Build login form
|
||||
Widget _buildLoginForm() {
|
||||
return SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Text(tr("Please sign into your Comunic account: ")),
|
||||
Container(
|
||||
child: _buildErrorCard(),
|
||||
),
|
||||
//Email address
|
||||
TextField(
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: tr("Email address"),
|
||||
alignLabelWithHint: true,
|
||||
errorText: _currEmail.length > 0 && !validateEmail(_currEmail)
|
||||
? tr("Invalid email address!")
|
||||
: null),
|
||||
onChanged: _emailChanged,
|
||||
),
|
||||
|
||||
//Password
|
||||
TextField(
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: tr("Password"),
|
||||
alignLabelWithHint: true,
|
||||
),
|
||||
onChanged: _passwordChanged,
|
||||
),
|
||||
|
||||
RaisedButton(
|
||||
child: Text(tr("Sign in")),
|
||||
onPressed: !validateEmail(_currEmail) || _currPassword.length < 3
|
||||
? null
|
||||
: () => _submitForm(context),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Comunic"),
|
||||
),
|
||||
body: _buildLoginForm());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user