From c220a4883008cdae58aeef60873acbf6fc7cc526 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 3 Dec 2016 13:21:44 -0500 Subject: [PATCH] 15 --- lib/angel_client.dart | 4 +++- lib/browser.dart | 41 +++++++++++++++++++++++++++++++++-------- pubspec.yaml | 2 +- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/angel_client.dart b/lib/angel_client.dart index f77e3a62..81b10f21 100644 --- a/lib/angel_client.dart +++ b/lib/angel_client.dart @@ -14,7 +14,7 @@ abstract class Angel { Angel(String this.basePath); Future authenticate( - {String type: auth_types.LOCAL, + {String type, credentials, String authEndpoint: '/auth', String reviveEndpoint: '/auth/token'}); @@ -31,6 +31,8 @@ abstract class Angel { abstract class AngelAuthResult { Map get data; String get token; + + Map toJson(); } /// Queries a service on an Angel server, with the same API. diff --git a/lib/browser.dart b/lib/browser.dart index c85852b4..605ed677 100644 --- a/lib/browser.dart +++ b/lib/browser.dart @@ -33,6 +33,8 @@ _send(HttpRequest request, [data]) { if (data == null) request.send(); + else if (data is String) + request.send(data); else request.send(JSON.encode(data)); return completer.future; @@ -45,18 +47,25 @@ class Rest extends Angel { Rest(String basePath) : super(basePath); @override - Future authenticate( - {String type: auth_types.LOCAL, + Future authenticate( + {String type, credentials, String authEndpoint: '/auth', String reviveEndpoint: '/auth/token'}) async { if (type == null) { + if (!window.localStorage.containsKey('token')) { + throw new Exception( + 'Cannot revive token from localStorage - there is none.'); + } + final result = new _AngelAuthResultImpl( token: JSON.decode(window.localStorage['token']), data: JSON.decode(window.localStorage['user'])); final completer = new Completer(); - final request = new HttpRequest(); + final request = new HttpRequest()..responseType = 'json'; request.open('POST', '$basePath$reviveEndpoint'); + request.setRequestHeader('Accept', 'application/json'); + request.setRequestHeader('Content-Type', 'application/json'); request.setRequestHeader('Authorization', 'Bearer ${result.token}'); request @@ -76,7 +85,7 @@ class Rest extends Angel { } }); - request.send(); + request.send(JSON.encode(result)); return completer.future; } @@ -130,15 +139,31 @@ abstract class RestService extends Service { } class _AngelAuthResultImpl implements AngelAuthResult { + String _token; final Map data = {}; - final String token; + String get token => _token; + + _AngelAuthResultImpl({token, Map data: const {}}) { + if (token is String) _token = token; - _AngelAuthResultImpl({this.token, Map data: const {}}) { this.data.addAll(data ?? {}); } - factory _AngelAuthResultImpl.fromMap(Map data) => - new _AngelAuthResultImpl(token: data['token'], data: data['data']); + factory _AngelAuthResultImpl.fromMap(Map data) { + final result = new _AngelAuthResultImpl(); + + if (data is Map && data.containsKey('token') && data['token'] is String) + result._token = data['token']; + + if (data is Map) result.data.addAll(data['data'] ?? {}); + + return result; + } + + @override + Map toJson() { + return {'token': token, 'data': data}; + } } /// Queries an Angel service via REST. diff --git a/pubspec.yaml b/pubspec.yaml index 14bf3b0a..d9b1ce82 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_client -version: 1.0.0-dev+12 +version: 1.0.0-dev+15 description: Client library for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_client