2018-09-17 13:35:57 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2018-09-22 20:56:16 +00:00
|
|
|
import 'package:vikunja_app/api/client.dart';
|
|
|
|
import 'package:vikunja_app/api/service.dart';
|
|
|
|
import 'package:vikunja_app/models/user.dart';
|
|
|
|
import 'package:vikunja_app/service/services.dart';
|
2018-09-17 13:35:57 +00:00
|
|
|
|
2018-09-17 16:16:50 +00:00
|
|
|
class UserAPIService extends APIService implements UserService {
|
|
|
|
UserAPIService(Client client) : super(client);
|
2018-09-17 13:35:57 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<UserTokenPair> login(String username, password) async {
|
2018-09-17 16:16:50 +00:00
|
|
|
var token = await client.post('/login', body: {
|
2018-09-17 13:35:57 +00:00
|
|
|
'username': username,
|
|
|
|
'password': password
|
2021-06-04 09:34:25 +00:00
|
|
|
}).then((response) => response.body['token']);
|
2018-09-17 16:16:50 +00:00
|
|
|
return UserAPIService(Client(token, client.base))
|
2018-09-17 13:35:57 +00:00
|
|
|
.getCurrentUser()
|
|
|
|
.then((user) => UserTokenPair(user, token));
|
|
|
|
}
|
|
|
|
|
2018-10-08 14:26:01 +00:00
|
|
|
@override
|
|
|
|
Future<UserTokenPair> register(String username, email, password) async {
|
|
|
|
var newUser = await client.post('/register', body: {
|
|
|
|
'username': username,
|
|
|
|
'email': email,
|
|
|
|
'password': password
|
2022-04-20 20:57:21 +00:00
|
|
|
}).then((resp) => resp.body['username']);
|
2018-10-08 14:26:01 +00:00
|
|
|
return login(newUser, password);
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:35:57 +00:00
|
|
|
@override
|
|
|
|
Future<User> getCurrentUser() {
|
2022-04-20 20:57:21 +00:00
|
|
|
return client.get('/user').then((map) => User.fromJson(map.body));
|
2018-09-17 13:35:57 +00:00
|
|
|
}
|
|
|
|
}
|