+12
This commit is contained in:
parent
ac83e14f7f
commit
8b89208445
3 changed files with 36 additions and 14 deletions
|
@ -13,7 +13,11 @@ abstract class Angel {
|
|||
|
||||
Angel(String this.basePath);
|
||||
|
||||
Future<AngelAuthResult> authenticate({String type: auth_types.LOCAL, credentials, String authEndpoint: '/auth'});
|
||||
Future<AngelAuthResult> authenticate(
|
||||
{String type: auth_types.LOCAL,
|
||||
credentials,
|
||||
String authEndpoint: '/auth',
|
||||
String reviveEndpoint: '/auth/token'});
|
||||
|
||||
/// Applies an [AngelConfigurer] to this instance.
|
||||
Future configure(AngelConfigurer configurer) async {
|
||||
|
|
|
@ -48,21 +48,39 @@ class Rest extends Angel {
|
|||
Future<AngelAuthResult> authenticate(
|
||||
{String type: auth_types.LOCAL,
|
||||
credentials,
|
||||
String authEndpoint: '/auth'}) async {
|
||||
String authEndpoint: '/auth',
|
||||
String reviveEndpoint: '/auth/token'}) async {
|
||||
if (type == null) {
|
||||
if (window.localStorage.containsKey('user') &&
|
||||
window.localStorage.containsKey('token')) {
|
||||
final result = new _AngelAuthResultImpl(
|
||||
token: JSON.decode(window.localStorage['token']),
|
||||
data: JSON.decode(window.localStorage['user']));
|
||||
_authToken = result.token;
|
||||
return result;
|
||||
} else {
|
||||
throw new Exception('Failed to authenticate via localStorage.');
|
||||
}
|
||||
final result = new _AngelAuthResultImpl(
|
||||
token: JSON.decode(window.localStorage['token']),
|
||||
data: JSON.decode(window.localStorage['user']));
|
||||
final completer = new Completer();
|
||||
final request = new HttpRequest();
|
||||
request.open('POST', '$basePath$reviveEndpoint');
|
||||
request.setRequestHeader('Authorization', 'Bearer ${result.token}');
|
||||
|
||||
request
|
||||
..onLoadEnd.listen((_) {
|
||||
final result = new _AngelAuthResultImpl.fromMap(request.response);
|
||||
_authToken = result.token;
|
||||
window.localStorage['token'] = JSON.encode(result.token);
|
||||
window.localStorage['user'] = JSON.encode(result.data);
|
||||
completer.complete(result);
|
||||
})
|
||||
..onError.listen((_) {
|
||||
try {
|
||||
throw new Exception(
|
||||
'Request failed with status code ${request.status}.');
|
||||
} catch (e, st) {
|
||||
completer.completeError(e, st);
|
||||
}
|
||||
});
|
||||
|
||||
request.send();
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
final url = '$authEndpoint/$type';
|
||||
final url = '$basePath$authEndpoint/$type';
|
||||
|
||||
if (type == auth_types.LOCAL) {
|
||||
final completer = new Completer();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
name: angel_client
|
||||
version: 1.0.0-dev+11
|
||||
version: 1.0.0-dev+12
|
||||
description: Client library for the Angel framework.
|
||||
author: Tobe O <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/angel_client
|
||||
|
|
Loading…
Reference in a new issue