This commit is contained in:
thosakwe 2016-11-28 19:42:02 -05:00
parent ac83e14f7f
commit 8b89208445
3 changed files with 36 additions and 14 deletions

View file

@ -13,7 +13,11 @@ abstract class Angel {
Angel(String this.basePath); 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. /// Applies an [AngelConfigurer] to this instance.
Future configure(AngelConfigurer configurer) async { Future configure(AngelConfigurer configurer) async {

View file

@ -48,21 +48,39 @@ class Rest extends Angel {
Future<AngelAuthResult> authenticate( Future<AngelAuthResult> authenticate(
{String type: auth_types.LOCAL, {String type: auth_types.LOCAL,
credentials, credentials,
String authEndpoint: '/auth'}) async { String authEndpoint: '/auth',
String reviveEndpoint: '/auth/token'}) async {
if (type == null) { if (type == null) {
if (window.localStorage.containsKey('user') &&
window.localStorage.containsKey('token')) {
final result = new _AngelAuthResultImpl( final result = new _AngelAuthResultImpl(
token: JSON.decode(window.localStorage['token']), token: JSON.decode(window.localStorage['token']),
data: JSON.decode(window.localStorage['user'])); 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; _authToken = result.token;
return result; window.localStorage['token'] = JSON.encode(result.token);
} else { window.localStorage['user'] = JSON.encode(result.data);
throw new Exception('Failed to authenticate via localStorage.'); 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) { if (type == auth_types.LOCAL) {
final completer = new Completer(); final completer = new Completer();

View file

@ -1,5 +1,5 @@
name: angel_client name: angel_client
version: 1.0.0-dev+11 version: 1.0.0-dev+12
description: Client library for the Angel framework. description: Client library for the Angel framework.
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_client homepage: https://github.com/angel-dart/angel_client