This commit is contained in:
thosakwe 2017-02-28 16:56:59 -05:00
parent 3e52ed6291
commit bea2aa03f9
6 changed files with 41 additions and 16 deletions

View file

@ -1,6 +1,6 @@
# angel_client
[![pub 1.0.0](https://img.shields.io/badge/pub-1.0.0-brightgreen.svg)](https://pub.dartlang.org/packages/angel_client)
[![pub 1.0.1](https://img.shields.io/badge/pub-1.0.1-brightgreen.svg)](https://pub.dartlang.org/packages/angel_client)
[![build status](https://travis-ci.org/angel-dart/client.svg)](https://travis-ci.org/angel-dart/client)
Client library for the Angel framework.

View file

@ -17,7 +17,7 @@ typedef AngelDeserializer(x);
/// Represents an Angel server that we are querying.
abstract class Angel {
String get authToken;
String authToken;
String basePath;
Angel(String this.basePath);
@ -28,6 +28,9 @@ abstract class Angel {
String authEndpoint: '/auth',
String reviveEndpoint: '/auth/token'});
/// Opens the [url] in a new window, and returns a [Stream] that will fire a JWT on successful authentication.
Stream<String> authenticateViaPopup(String url, {String eventName: 'token'});
Future close();
/// Applies an [AngelConfigurer] to this instance.
@ -37,21 +40,17 @@ abstract class Angel {
Service service<T>(String path, {Type type, AngelDeserializer deserializer});
Future<http.Response> delete(String url,
{Map<String, String> headers});
Future<http.Response> delete(String url, {Map<String, String> headers});
Future<http.Response> get(String url, {Map<String, String> headers});
Future<http.Response> head(String url, {Map<String, String> headers});
Future<http.Response> patch(String url,
{body, Map<String, String> headers});
Future<http.Response> patch(String url, {body, Map<String, String> headers});
Future<http.Response> post(String url,
{body, Map<String, String> headers});
Future<http.Response> post(String url, {body, Map<String, String> headers});
Future<http.Response> put(String url,
{body, Map<String, String> headers});
Future<http.Response> put(String url, {body, Map<String, String> headers});
}
/// Represents the result of authentication with an Angel server.

View file

@ -50,9 +50,6 @@ AngelHttpException failure(http.Response response, {error, StackTrace stack}) {
}
abstract class BaseAngelClient extends Angel {
@override
String authToken;
final http.BaseClient client;
BaseAngelClient(this.client, String basePath) : super(basePath);

View file

@ -1,9 +1,9 @@
/// Browser library for the Angel framework.
library angel_client.browser;
import 'dart:async' show Future;
import 'dart:async' show Future, Stream, StreamController;
import 'dart:convert' show JSON;
import 'dart:html' show window;
import 'dart:html' show CustomEvent, window;
import 'package:http/browser_client.dart' as http;
import 'angel_client.dart';
import 'auth_types.dart' as auth_types;
@ -45,4 +45,29 @@ class Rest extends BaseAngelClient {
return result;
}
}
@override
Stream<String> authenticateViaPopup(String url,
{String eventName: 'token', String errorMessage}) {
var ctrl = new StreamController<String>();
var wnd = window.open(url, 'angel_client_auth_popup');
wnd
..on['beforeunload'].listen((_) {
if (!ctrl.isClosed) {
ctrl.addError(new AngelHttpException.notAuthenticated(
message:
errorMessage ?? 'Authentication via popup window failed.'));
ctrl.close();
}
})
..on[eventName ?? 'token'].listen((CustomEvent e) {
if (!ctrl.isClosed) {
ctrl.add(e.detail);
ctrl.close();
}
});
return ctrl.stream;
}
}

View file

@ -18,6 +18,10 @@ class Rest extends BaseAngelClient {
return new RestService(
client, this, "$basePath/$uri", T != dynamic ? T : type);
}
@override
Stream<String> authenticateViaPopup(String url, {String eventName: 'token'}) {
throw new UnimplementedError('Opening popup windows is not supported in the `dart:io` client.');
}
}
/// Queries an Angel service via REST.

View file

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