diff --git a/README.md b/README.md index 9a8d60a1..5b097e3a 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/angel_client.dart b/lib/angel_client.dart index 86a808e1..00acef77 100644 --- a/lib/angel_client.dart +++ b/lib/angel_client.dart @@ -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 authenticateViaPopup(String url, {String eventName: 'token'}); + Future close(); /// Applies an [AngelConfigurer] to this instance. @@ -37,21 +40,17 @@ abstract class Angel { Service service(String path, {Type type, AngelDeserializer deserializer}); - Future delete(String url, - {Map headers}); + Future delete(String url, {Map headers}); Future get(String url, {Map headers}); Future head(String url, {Map headers}); - Future patch(String url, - {body, Map headers}); + Future patch(String url, {body, Map headers}); - Future post(String url, - {body, Map headers}); + Future post(String url, {body, Map headers}); - Future put(String url, - {body, Map headers}); + Future put(String url, {body, Map headers}); } /// Represents the result of authentication with an Angel server. diff --git a/lib/base_angel_client.dart b/lib/base_angel_client.dart index 728df035..639f6d2c 100644 --- a/lib/base_angel_client.dart +++ b/lib/base_angel_client.dart @@ -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); diff --git a/lib/browser.dart b/lib/browser.dart index e112f20c..231c2340 100644 --- a/lib/browser.dart +++ b/lib/browser.dart @@ -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 authenticateViaPopup(String url, + {String eventName: 'token', String errorMessage}) { + var ctrl = new StreamController(); + 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; + } } diff --git a/lib/io.dart b/lib/io.dart index caec159e..b1ec9f1f 100644 --- a/lib/io.dart +++ b/lib/io.dart @@ -18,6 +18,10 @@ class Rest extends BaseAngelClient { return new RestService( client, this, "$basePath/$uri", T != dynamic ? T : type); } + @override + Stream 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. diff --git a/pubspec.yaml b/pubspec.yaml index e7f88483..2fab6c13 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 homepage: https://github.com/angel-dart/angel_client