platform/packages/client/lib/browser.dart

80 lines
2.3 KiB
Dart
Raw Permalink Normal View History

/// Browser client library for the Protevus 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;
import 'protevus_client.dart';
2017-03-29 01:52:19 +00:00
// import 'auth_types.dart' as auth_types;
import 'base_protevus_client.dart';
export 'protevus_client.dart';
2016-06-25 18:37:49 +00:00
/// Queries an Protevus server via REST.
class Rest extends BaseProtevusClient {
2021-04-10 13:22:20 +00:00
Rest(String basePath) : super(http.BrowserClient(), basePath);
2016-11-28 03:28:41 +00:00
2021-04-10 13:22:20 +00:00
@override
Future<ProtevusAuthResult> authenticate(
{String? type, credentials, String authEndpoint = '/auth'}) async {
2019-01-06 02:08:31 +00:00
if (type == null || type == 'token') {
2016-12-03 18:21:44 +00:00
if (!window.localStorage.containsKey('token')) {
2021-04-10 13:22:20 +00:00
throw Exception(
2016-12-03 18:21:44 +00:00
'Cannot revive token from localStorage - there is none.');
}
2021-04-10 13:22:20 +00:00
var token = json.decode(window.localStorage['token']!);
2019-01-06 02:08:31 +00:00
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,
2021-04-10 13:22:20 +00:00
{String eventName = 'token', String? errorMessage}) {
var ctrl = StreamController<String>();
2017-02-28 21:56:59 +00:00
var wnd = window.open(url, 'angel_client_auth_popup');
2017-03-29 01:52:19 +00:00
Timer t;
2021-04-10 13:22:20 +00:00
StreamSubscription? sub;
t = Timer.periodic(Duration(milliseconds: 500), (timer) {
2017-03-17 19:21:09 +00:00
if (!ctrl.isClosed) {
2021-04-10 13:22:20 +00:00
if (wnd.closed!) {
ctrl.addError(ProtevusHttpException.notAuthenticated(
2017-02-28 21:56:59 +00:00
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
}
2021-04-10 13:22:20 +00:00
} else {
2017-03-17 19:21:09 +00:00
timer.cancel();
2021-04-10 13:22:20 +00:00
}
2017-03-17 19:21:09 +00:00
});
2021-04-10 13:22:20 +00:00
sub = window.on[eventName].listen((Event ev) {
2017-06-30 22:57:01 +00:00
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();
2021-04-10 13:22:20 +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
}