From 8b892084456261464f1e896f6bf7af010b5907d8 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Mon, 28 Nov 2016 19:42:02 -0500 Subject: [PATCH] +12 --- lib/angel_client.dart | 6 +++++- lib/browser.dart | 42 ++++++++++++++++++++++++++++++------------ pubspec.yaml | 2 +- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/lib/angel_client.dart b/lib/angel_client.dart index 9fdce34b..f77e3a62 100644 --- a/lib/angel_client.dart +++ b/lib/angel_client.dart @@ -13,7 +13,11 @@ abstract class Angel { Angel(String this.basePath); - Future authenticate({String type: auth_types.LOCAL, credentials, String authEndpoint: '/auth'}); + Future 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 { diff --git a/lib/browser.dart b/lib/browser.dart index 02752dfb..c85852b4 100644 --- a/lib/browser.dart +++ b/lib/browser.dart @@ -48,21 +48,39 @@ class Rest extends Angel { Future 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(); diff --git a/pubspec.yaml b/pubspec.yaml index e7f154a3..14bf3b0a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 homepage: https://github.com/angel-dart/angel_client