platform/lib/browser.dart

91 lines
2.8 KiB
Dart
Raw Normal View History

2017-06-30 22:57:01 +00:00
/// Browser client library for the Angel framework.
2016-06-25 18:37:49 +00:00
library angel_client.browser;
2017-06-03 17:46:49 +00:00
import 'dart:async' show Future, Stream, StreamController, StreamSubscription, Timer;
2016-06-25 18:37:49 +00:00
import 'dart:convert' show JSON;
2017-06-30 22:57:01 +00:00
import 'dart:html' show CustomEvent, Event, window;
2016-12-09 00:24:07 +00:00
import 'package:http/browser_client.dart' as http;
2016-09-03 12:02:32 +00:00
import 'angel_client.dart';
2017-03-29 01:52:19 +00:00
// import 'auth_types.dart' as auth_types;
2016-12-09 00:24:07 +00:00
import 'base_angel_client.dart';
2016-09-03 12:02:32 +00:00
export 'angel_client.dart';
2016-06-25 18:37:49 +00:00
/// Queries an Angel server via REST.
2016-12-09 00:24:07 +00:00
class Rest extends BaseAngelClient {
Rest(String basePath) : super(new http.BrowserClient(), basePath);
2016-11-28 03:28:41 +00:00
@override
2016-12-09 00:24:07 +00:00
Future<AngelAuthResult> authenticate(
2017-03-07 21:54:13 +00:00
{String type,
2016-11-28 03:28:41 +00:00
credentials,
2016-11-29 00:42:02 +00:00
String authEndpoint: '/auth',
String reviveEndpoint: '/auth/token'}) async {
2016-11-28 03:34:04 +00:00
if (type == null) {
2016-12-03 18:21:44 +00:00
if (!window.localStorage.containsKey('token')) {
throw new Exception(
'Cannot revive token from localStorage - there is none.');
}
2016-12-09 00:24:07 +00:00
try {
final result = await super.authenticate(
2017-03-07 21:54:13 +00:00
type: null,
2016-12-09 00:24:07 +00:00
credentials: {'token': JSON.decode(window.localStorage['token'])},
reviveEndpoint: reviveEndpoint);
window.localStorage['token'] = JSON.encode(authToken = result.token);
window.localStorage['user'] = JSON.encode(result.data);
return result;
} catch (e, st) {
throw new AngelHttpException(e,
message: 'Failed to revive auth token.', stackTrace: st);
}
2016-11-28 03:28:41 +00:00
} else {
2016-12-09 00:24:07 +00:00
final result = await super.authenticate(
type: type, credentials: credentials, authEndpoint: authEndpoint);
window.localStorage['token'] = JSON.encode(authToken = result.token);
window.localStorage['user'] = JSON.encode(result.data);
return result;
2016-11-28 03:28:41 +00:00
}
}
2017-02-28 21:56:59 +00:00
@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');
2017-03-29 01:52:19 +00:00
Timer t;
2017-12-07 06:41:40 +00:00
StreamSubscription<CustomEvent> sub;
2017-03-29 01:52:19 +00:00
t = new Timer.periodic(new Duration(milliseconds: 500), (timer) {
2017-03-17 19:21:09 +00:00
if (!ctrl.isClosed) {
if (wnd.closed) {
2017-02-28 21:56:59 +00:00
ctrl.addError(new AngelHttpException.notAuthenticated(
message:
errorMessage ?? 'Authentication via popup window failed.'));
ctrl.close();
2017-03-29 01:52:19 +00:00
timer.cancel();
2017-06-03 17:46:49 +00:00
sub?.cancel();
2017-02-28 21:56:59 +00:00
}
2017-03-17 19:21:09 +00:00
} else
timer.cancel();
});
2017-06-30 22:57:01 +00:00
sub = window.on[eventName ?? 'token'].listen((Event ev) {
var e = ev as CustomEvent;
2017-03-17 19:21:09 +00:00
if (!ctrl.isClosed) {
ctrl.add(e.detail);
2017-03-29 01:52:19 +00:00
t.cancel();
2017-03-17 19:21:09 +00:00
ctrl.close();
2017-06-03 17:46:49 +00:00
sub.cancel();
2017-03-17 19:21:09 +00:00
}
});
2017-02-28 21:56:59 +00:00
return ctrl.stream;
}
2017-03-29 01:52:19 +00:00
@override
Future logout() {
window.localStorage.remove('token');
return super.logout();
}
2016-06-25 18:37:49 +00:00
}