Added register (#13)
the build was successful Details

This commit is contained in:
konrad 2018-10-08 14:26:01 +00:00 committed by Gitea
parent 301997f32b
commit abf0196de3
7 changed files with 199 additions and 9 deletions

View File

@ -19,6 +19,16 @@ class UserAPIService extends APIService implements UserService {
.then((user) => UserTokenPair(user, token));
}
@override
Future<UserTokenPair> register(String username, email, password) async {
var newUser = await client.post('/register', body: {
'username': username,
'email': email,
'password': password
}).then((resp) => resp['username']);
return login(newUser, password);
}
@override
Future<User> getCurrentUser() {
return client.get('/user').then((map) => User.fromJson(map));

View File

@ -37,8 +37,7 @@ class VikunjaGlobalState extends State<VikunjaGlobal> {
Client get client => _client;
UserManager get userManager => new UserManager(_storage);
UserService get userService => new UserAPIService(_client);
UserService newLoginService(base) => new UserAPIService(Client(null, base));
UserService newUserService(base) => new UserAPIService(Client(null, base));
NamespaceService get namespaceService => new NamespaceAPIService(client);
TaskService get taskService => new TaskAPIService(client);
ListService get listService => new ListAPIService(client);

View File

@ -1,15 +1,13 @@
import 'package:flutter/material.dart';
import 'package:vikunja_app/global.dart';
import 'package:vikunja_app/main.dart';
import 'package:vikunja_app/pages/register_page.dart';
import 'package:vikunja_app/utils/validator.dart';
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
final RegExp _url = new RegExp(
r'https?:\/\/((([a-zA-Z0-9.\-\_]+)\.[a-zA-Z]+)|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(:[0-9]+)?');
class _LoginPageState extends State<LoginPage> {
final _formKey = GlobalKey<FormState>();
String _server, _username, _password;
@ -44,8 +42,7 @@ class _LoginPageState extends State<LoginPage> {
child: TextFormField(
onSaved: (serverAddress) => _server = serverAddress,
validator: (address) {
var hasMatch = _url.hasMatch(address);
return hasMatch ? null : 'Invalid URL';
return isUrl(address) ? null : 'Invalid URL';
},
decoration: new InputDecoration(
labelText: 'Server Address'),
@ -85,6 +82,19 @@ class _LoginPageState extends State<LoginPage> {
? CircularProgressIndicator()
: Text('Login'),
))),
Builder(
builder: (context) => ButtonTheme(
height: _loading ? 55.0 : 36.0,
child: RaisedButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
RegisterPage())),
child: _loading
? CircularProgressIndicator()
: Text('Register'),
))),
],
)),
),
@ -96,7 +106,7 @@ class _LoginPageState extends State<LoginPage> {
try {
var vGlobal = VikunjaGlobal.of(context);
var newUser =
await vGlobal.newLoginService(_server).login(_username, _password);
await vGlobal.newUserService(_server).login(_username, _password);
vGlobal.changeUser(newUser.user, token: newUser.token, base: _server);
} catch (ex) {
showDialog(

View File

@ -0,0 +1,152 @@
import 'package:flutter/material.dart';
import 'package:vikunja_app/global.dart';
import 'package:vikunja_app/utils/validator.dart';
class RegisterPage extends StatefulWidget {
@override
_RegisterPageState createState() => _RegisterPageState();
}
class _RegisterPageState extends State<RegisterPage> {
final _formKey = GlobalKey<FormState>();
final passwordController = TextEditingController();
String _server, _username, _email, _password;
bool _loading = false;
@override
Widget build(BuildContext ctx) {
return Scaffold(
appBar: AppBar(
title: Text('Register to Vikunja'),
),
body: Builder(
builder: (BuildContext context) => SafeArea(
top: false,
bottom: false,
child: Form(
key: _formKey,
child: ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Image(
image: AssetImage('assets/vikunja_logo.png'),
height: 128.0,
semanticLabel: 'Vikunja Logo',
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
onSaved: (serverAddress) => _server = serverAddress,
validator: (address) {
return isUrl(address) ? null : 'Invalid URL';
},
decoration: new InputDecoration(
labelText: 'Server Address'),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
onSaved: (username) => _username = username.trim(),
validator: (username) {
return username.trim().isNotEmpty ? null : 'Please specify a username';
},
decoration:
new InputDecoration(labelText: 'Username'),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
onSaved: (email) => _email = email,
validator: (email) {
return isEmail(email)
? null
: 'Email adress is invalid';
},
decoration:
new InputDecoration(labelText: 'Email Address'),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: passwordController,
onSaved: (password) => _password = password,
validator: (password) {
return password.length >= 8 ? null : 'Please use at least 8 characters';
},
decoration:
new InputDecoration(labelText: 'Password'),
obscureText: true,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
validator: (password) {
return passwordController.text == password
? null
: 'Passwords don\'t match.';
},
decoration: new InputDecoration(
labelText: 'Repeat Password'),
obscureText: true,
),
),
Builder(
builder: (context) => ButtonTheme(
height: _loading ? 55.0 : 36.0,
child: RaisedButton(
onPressed: !_loading
? () {
if (_formKey.currentState
.validate()) {
Form.of(context).save();
_registerUser(context);
} else {
print("awhat");
}
}
: null,
child: _loading
? CircularProgressIndicator()
: Text('Register'),
))),
],
)),
),
));
}
_registerUser(BuildContext context) async {
setState(() => _loading = true);
try {
var vGlobal = VikunjaGlobal.of(context);
var newUserLoggedIn = await vGlobal
.newUserService(_server)
.register(_username, _email, _password);
vGlobal.changeUser(newUserLoggedIn.user,
token: newUserLoggedIn.token, base: _server);
} catch (ex) {
showDialog(
context: context,
builder: (context) => new AlertDialog(
title: const Text(
'Registration failed! Please check your server url and credentials.'),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.pop(context),
child: const Text('CLOSE'))
],
));
} finally {
setState(() {
_loading = false;
});
}
}
}

View File

@ -151,6 +151,11 @@ class MockedUserService implements UserService {
return Future.value(UserTokenPair(_users[1], 'abcdefg'));
}
@override
Future<UserTokenPair> register(String username, email, password) {
return Future.value(UserTokenPair(_users[1], 'abcdefg'));
}
@override
Future<User> getCurrentUser() {
return Future.value(_users[1]);

View File

@ -29,5 +29,6 @@ abstract class TaskService {
abstract class UserService {
Future<UserTokenPair> login(String username, password);
Future<UserTokenPair> register(String username, email, password);
Future<User> getCurrentUser();
}

13
lib/utils/validator.dart Normal file
View File

@ -0,0 +1,13 @@
final RegExp _emailRegex = new RegExp(
r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$');
bool isEmail(email) {
return _emailRegex.hasMatch(email);
}
final RegExp _url = new RegExp(
r'https?:\/\/((([a-zA-Z0-9.\-\_]+)\.[a-zA-Z]+)|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(:[0-9]+)?');
bool isUrl(url) {
return _url.hasMatch(url);
}