Allow using a custom wger instance for login

This commit is contained in:
Roland Geider
2021-03-14 22:56:24 +01:00
parent c56911bb11
commit 8c710fb2b0
4 changed files with 163 additions and 31 deletions

View File

@@ -1,5 +1,59 @@
{
"@@last_modified": "2020-11-11T15:04:05.523531",
"login": "Login",
"@login": {
"description": "Text for login button"
},
"register": "Register",
"@register": {
"description": "Text for registration button"
},
"useDefaultServer": "Use default server",
"@useDefaultServer": {
"description": "Toggle button allowing users to switch between the default and a custom wger server"
},
"useCustomServer": "Use custom server",
"@useCustomServer": {
"description": "Toggle button allowing users to switch between the default and a custom wger server"
},
"invalidUrl": "Please enter a valid URL",
"@invalidUrl": {
"description": "Error message when the user enters an invalid URL, e.g. in the login form"
},
"passwordsDontMatch": "The passwords don't match",
"@passwordsDontMatch": {
"description": "Error message when the user enters two different passwords during registration"
},
"passwordTooShort": "The password is too short",
"@passwordTooShort": {
"description": "Error message when the user a password that is too short"
},
"password": "Password",
"confirmPassword": "Confirm password",
"invalidEmail": "Please enter a valid email address",
"@invalidEmail": {
"description": "Error message when the user enters an invalid email"
},
"email": "Email",
"username": "Username",
"invalidUsername": "Please enter a valid username",
"@invalidUsername": {
"description": "Error message when the user enters an invalid username"
},
"customServerUrl": "URL of the wger instance",
"@customServerUrl": {
"description": "Label in the form where the users can enter their own wger instance"
},
"customServerHint": "Enter the the address of your own server, otherwise the default one will be used",
"@customServerHint": {
"description": "Hint text for the form where the users can enter their own wger instance"
},
"reset": "Reset",
"@reset": {
"description": "Button text allowing the user to reset the entered values to the default"
},
"registerInstead": "Register instead",
"loginInstead": "Login instead",
"labelWorkoutPlans": "Workout plans",
"@labelWorkoutPlans": {
"description": "Title for screen workout plans"

View File

@@ -32,6 +32,8 @@ import 'package:wger/models/http_exception.dart';
import 'helpers.dart';
final DEFAULT_SERVER = 'https://wger.rge.uber.space';
class Auth with ChangeNotifier {
String token;
String serverUrl;
@@ -115,7 +117,7 @@ class Auth with ChangeNotifier {
/// Authenticates a user
Future<void> login(String username, String password, String serverUrl) async {
final uri = Uri.http(serverUrl, '/api/v2/login/');
final uri = Uri.parse(serverUrl + '/api/v2/login/');
await logout();
try {
@@ -152,15 +154,30 @@ class Auth with ChangeNotifier {
'serverUrl': this.serverUrl,
// 'expiryDate': _expiryDate.toIso8601String(),
});
final serverData = json.encode({
'serverUrl': this.serverUrl,
});
await setServerVersion();
await setApplicationVersion();
prefs.setString('userData', userData);
prefs.setString('lastServer', serverData);
} catch (error) {
throw error;
}
}
/// Loads the last server URL from which the user successfully logged in
Future<String> getServerUrlFromPrefs() async {
final prefs = await SharedPreferences.getInstance();
if (!prefs.containsKey('lastServer')) {
return DEFAULT_SERVER;
}
final userData = json.decode(prefs.getString('lastServer')) as Map<String, Object>;
return userData['serverUrl'];
}
Future<bool> tryAutoLogin() async {
final prefs = await SharedPreferences.getInstance();
if (!prefs.containsKey('userData')) {

View File

@@ -17,6 +17,7 @@
*/
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
import 'package:wger/helpers/ui.dart';
import 'package:wger/models/http_exception.dart';
@@ -93,6 +94,7 @@ class AuthCard extends StatefulWidget {
class _AuthCardState extends State<AuthCard> {
final GlobalKey<FormState> _formKey = GlobalKey();
AuthMode _authMode = AuthMode.Login;
bool _hideCustomServer = true;
Map<String, String> _authData = {
'username': '',
'email': '',
@@ -104,7 +106,16 @@ class _AuthCardState extends State<AuthCard> {
final _passwordController = TextEditingController(text: 'adminadmin');
final _password2Controller = TextEditingController();
final _emailController = TextEditingController();
final _serverUrlController = TextEditingController(text: 'http://10.0.2.2:8000');
final _serverUrlController = TextEditingController(text: DEFAULT_SERVER);
@override
void initState() {
//_serverUrlController.text
super.initState();
Provider.of<Auth>(context, listen: false).getServerUrlFromPrefs().then((value) {
_serverUrlController.text = value;
});
}
void _submit(BuildContext context) async {
if (!_formKey.currentState.validate()) {
@@ -178,14 +189,14 @@ class _AuthCardState extends State<AuthCard> {
children: <Widget>[
TextFormField(
key: Key('inputUsername'),
decoration: InputDecoration(labelText: 'Username'),
decoration: InputDecoration(labelText: AppLocalizations.of(context).username),
autofillHints: [AutofillHints.username],
controller: _usernameController,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value.isEmpty) {
return 'Invalid Username!';
return AppLocalizations.of(context).invalidUsername;
}
return null;
},
@@ -196,7 +207,7 @@ class _AuthCardState extends State<AuthCard> {
if (_authMode == AuthMode.Signup)
TextFormField(
key: Key('inputEmail'),
decoration: InputDecoration(labelText: 'E-Mail'),
decoration: InputDecoration(labelText: AppLocalizations.of(context).email),
autofillHints: [AutofillHints.email],
controller: _emailController,
keyboardType: TextInputType.emailAddress,
@@ -205,7 +216,7 @@ class _AuthCardState extends State<AuthCard> {
// Email is not required
validator: (value) {
if (value.isNotEmpty && !value.contains('@')) {
return 'Invalid email!';
return AppLocalizations.of(context).invalidEmail;
}
return null;
},
@@ -215,17 +226,15 @@ class _AuthCardState extends State<AuthCard> {
),
TextFormField(
key: Key('inputPassword'),
decoration: InputDecoration(labelText: 'Password'),
decoration: InputDecoration(labelText: AppLocalizations.of(context).password),
autofillHints: [AutofillHints.password],
obscureText: true,
controller: _passwordController,
textInputAction: TextInputAction.next,
validator: (value) {
/*
if (value.isEmpty || value.length < 8) {
return 'Password is too short!';
return AppLocalizations.of(context).passwordTooShort;
}
*/
return null;
},
onSaved: (value) {
@@ -235,32 +244,67 @@ class _AuthCardState extends State<AuthCard> {
if (_authMode == AuthMode.Signup)
TextFormField(
key: Key('inputPassword2'),
decoration: InputDecoration(labelText: 'Confirm Password'),
decoration:
InputDecoration(labelText: AppLocalizations.of(context).confirmPassword),
controller: _password2Controller,
enabled: _authMode == AuthMode.Signup,
obscureText: true,
validator: _authMode == AuthMode.Signup
? (value) {
if (value != _passwordController.text) {
return 'Passwords do not match!';
return AppLocalizations.of(context).passwordsDontMatch;
}
return null;
}
: null,
),
TextFormField(
key: Key('inputServer'),
decoration: InputDecoration(labelText: 'Server URL'),
controller: _serverUrlController,
validator: (value) {
if (value.isEmpty || !value.contains('http')) {
return 'Invalid URL!';
}
return null;
},
onSaved: (value) {
_authData['serverUrl'] = value;
},
// Off-stage widgets are kept in the tree, otherwise the server URL
// would not be saved to _authData
Offstage(
offstage: _hideCustomServer,
child: Row(
children: [
Flexible(
flex: 3,
child: TextFormField(
key: Key('inputServer'),
decoration: InputDecoration(
labelText: AppLocalizations.of(context).customServerUrl,
helperText: AppLocalizations.of(context).customServerHint,
helperMaxLines: 4),
controller: _serverUrlController,
validator: (value) {
if (Uri.tryParse(value) == null) {
return AppLocalizations.of(context).invalidUrl;
}
if (value.isEmpty || !value.contains('http')) {
return AppLocalizations.of(context).invalidUrl;
}
return null;
},
onSaved: (value) {
_authData['serverUrl'] = value;
},
),
),
SizedBox(
width: 20,
),
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: const Icon(Icons.undo),
onPressed: () {
_serverUrlController.text = DEFAULT_SERVER;
},
),
Text(AppLocalizations.of(context).reset)
],
),
],
),
),
SizedBox(
height: 20,
@@ -270,16 +314,33 @@ class _AuthCardState extends State<AuthCard> {
else
ElevatedButton(
key: Key('actionButton'),
child: Text(_authMode == AuthMode.Login ? 'LOGIN' : 'REGISTER'),
child: Text(_authMode == AuthMode.Login
? AppLocalizations.of(context).login
: AppLocalizations.of(context).register),
onPressed: () {
return _submit(context);
},
),
TextButton(
key: Key('toggleActionButton'),
child: Text('${_authMode == AuthMode.Login ? 'REGISTER' : 'LOGIN'} INSTEAD'),
child: Text(
_authMode == AuthMode.Login
? AppLocalizations.of(context).registerInstead.toUpperCase()
: AppLocalizations.of(context).loginInstead.toUpperCase(),
),
onPressed: _switchAuthMode,
),
TextButton(
child: Text(_hideCustomServer
? AppLocalizations.of(context).useCustomServer
: AppLocalizations.of(context).useDefaultServer),
key: Key('toggleCustomServerButton'),
onPressed: () {
setState(() {
_hideCustomServer = !_hideCustomServer;
});
},
),
],
),
),

View File

@@ -70,7 +70,7 @@ packages:
name: build_daemon
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.9"
version: "2.1.10"
build_resolvers:
dependency: transitive
description:
@@ -398,7 +398,7 @@ packages:
name: mockito
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.0"
version: "5.0.1"
nested:
dependency: transitive
description:
@@ -725,7 +725,7 @@ packages:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.0.4"
xdg_directories:
dependency: transitive
description:
@@ -748,5 +748,5 @@ packages:
source: hosted
version: "3.1.0"
sdks:
dart: ">=2.12.0-259.9.beta <3.0.0"
dart: ">=2.12.0 <3.0.0"
flutter: ">=1.22.0"