15
This commit is contained in:
parent
8b89208445
commit
c220a48830
3 changed files with 37 additions and 10 deletions
|
@ -14,7 +14,7 @@ abstract class Angel {
|
||||||
Angel(String this.basePath);
|
Angel(String this.basePath);
|
||||||
|
|
||||||
Future<AngelAuthResult> authenticate(
|
Future<AngelAuthResult> authenticate(
|
||||||
{String type: auth_types.LOCAL,
|
{String type,
|
||||||
credentials,
|
credentials,
|
||||||
String authEndpoint: '/auth',
|
String authEndpoint: '/auth',
|
||||||
String reviveEndpoint: '/auth/token'});
|
String reviveEndpoint: '/auth/token'});
|
||||||
|
@ -31,6 +31,8 @@ abstract class Angel {
|
||||||
abstract class AngelAuthResult {
|
abstract class AngelAuthResult {
|
||||||
Map<String, dynamic> get data;
|
Map<String, dynamic> get data;
|
||||||
String get token;
|
String get token;
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Queries a service on an Angel server, with the same API.
|
/// Queries a service on an Angel server, with the same API.
|
||||||
|
|
|
@ -33,6 +33,8 @@ _send(HttpRequest request, [data]) {
|
||||||
|
|
||||||
if (data == null)
|
if (data == null)
|
||||||
request.send();
|
request.send();
|
||||||
|
else if (data is String)
|
||||||
|
request.send(data);
|
||||||
else
|
else
|
||||||
request.send(JSON.encode(data));
|
request.send(JSON.encode(data));
|
||||||
return completer.future;
|
return completer.future;
|
||||||
|
@ -45,18 +47,25 @@ class Rest extends Angel {
|
||||||
Rest(String basePath) : super(basePath);
|
Rest(String basePath) : super(basePath);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<AngelAuthResult> authenticate(
|
Future authenticate(
|
||||||
{String type: auth_types.LOCAL,
|
{String type,
|
||||||
credentials,
|
credentials,
|
||||||
String authEndpoint: '/auth',
|
String authEndpoint: '/auth',
|
||||||
String reviveEndpoint: '/auth/token'}) async {
|
String reviveEndpoint: '/auth/token'}) async {
|
||||||
if (type == null) {
|
if (type == null) {
|
||||||
|
if (!window.localStorage.containsKey('token')) {
|
||||||
|
throw new Exception(
|
||||||
|
'Cannot revive token from localStorage - there is none.');
|
||||||
|
}
|
||||||
|
|
||||||
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 completer = new Completer();
|
||||||
final request = new HttpRequest();
|
final request = new HttpRequest()..responseType = 'json';
|
||||||
request.open('POST', '$basePath$reviveEndpoint');
|
request.open('POST', '$basePath$reviveEndpoint');
|
||||||
|
request.setRequestHeader('Accept', 'application/json');
|
||||||
|
request.setRequestHeader('Content-Type', 'application/json');
|
||||||
request.setRequestHeader('Authorization', 'Bearer ${result.token}');
|
request.setRequestHeader('Authorization', 'Bearer ${result.token}');
|
||||||
|
|
||||||
request
|
request
|
||||||
|
@ -76,7 +85,7 @@ class Rest extends Angel {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
request.send();
|
request.send(JSON.encode(result));
|
||||||
return completer.future;
|
return completer.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,15 +139,31 @@ abstract class RestService extends Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _AngelAuthResultImpl implements AngelAuthResult {
|
class _AngelAuthResultImpl implements AngelAuthResult {
|
||||||
|
String _token;
|
||||||
final Map<String, dynamic> data = {};
|
final Map<String, dynamic> data = {};
|
||||||
final String token;
|
String get token => _token;
|
||||||
|
|
||||||
|
_AngelAuthResultImpl({token, Map<String, dynamic> data: const {}}) {
|
||||||
|
if (token is String) _token = token;
|
||||||
|
|
||||||
_AngelAuthResultImpl({this.token, Map<String, dynamic> data: const {}}) {
|
|
||||||
this.data.addAll(data ?? {});
|
this.data.addAll(data ?? {});
|
||||||
}
|
}
|
||||||
|
|
||||||
factory _AngelAuthResultImpl.fromMap(Map data) =>
|
factory _AngelAuthResultImpl.fromMap(Map data) {
|
||||||
new _AngelAuthResultImpl(token: data['token'], data: data['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<String, dynamic> toJson() {
|
||||||
|
return {'token': token, 'data': data};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Queries an Angel service via REST.
|
/// Queries an Angel service via REST.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name: angel_client
|
name: angel_client
|
||||||
version: 1.0.0-dev+12
|
version: 1.0.0-dev+15
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue