platform/lib/browser.dart

81 lines
2.4 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;
2018-08-26 22:41:01 +00:00
import 'dart:async'
show Future, Stream, StreamController, StreamSubscription, Timer;
2017-06-30 22:57:01 +00:00
import 'dart:html' show CustomEvent, Event, window;
2018-08-26 22:41:01 +00:00
import 'dart:convert';
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
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,
2019-01-06 02:08:31 +00:00
String authEndpoint = '/auth',
@deprecated String reviveEndpoint = '/auth/token'}) async {
if (type == null || type == 'token') {
2016-12-03 18:21:44 +00:00
if (!window.localStorage.containsKey('token')) {
throw new Exception(
'Cannot revive token from localStorage - there is none.');
}
2019-01-06 02:08:31 +00:00
var token = json.decode(window.localStorage['token']);
credentials ??= {'token': token};
2016-11-28 03:28:41 +00:00
}
2019-01-06 02:08:31 +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,
2019-01-06 02:08:31 +00:00
{String eventName = 'token', String errorMessage}) {
2017-02-28 21:56:59 +00:00
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-27 16:00:57 +00:00
StreamSubscription 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) {
2018-08-26 22:41:01 +00:00
ctrl.add(e.detail.toString());
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
}