platform/packages/client/lib/browser.dart
2024-10-12 18:45:27 -07:00

79 lines
2.3 KiB
Dart

/// Browser client library for the Protevus framework.
library angel_client.browser;
import 'dart:async'
show Future, Stream, StreamController, StreamSubscription, Timer;
import 'dart:html' show CustomEvent, Event, window;
import 'dart:convert';
import 'package:http/browser_client.dart' as http;
import 'protevus_client.dart';
// import 'auth_types.dart' as auth_types;
import 'base_protevus_client.dart';
export 'protevus_client.dart';
/// Queries an Protevus server via REST.
class Rest extends BaseProtevusClient {
Rest(String basePath) : super(http.BrowserClient(), basePath);
@override
Future<ProtevusAuthResult> authenticate(
{String? type, credentials, String authEndpoint = '/auth'}) async {
if (type == null || type == 'token') {
if (!window.localStorage.containsKey('token')) {
throw Exception(
'Cannot revive token from localStorage - there is none.');
}
var token = json.decode(window.localStorage['token']!);
credentials ??= {'token': token};
}
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;
}
@override
Stream<String> authenticateViaPopup(String url,
{String eventName = 'token', String? errorMessage}) {
var ctrl = StreamController<String>();
var wnd = window.open(url, 'angel_client_auth_popup');
Timer t;
StreamSubscription? sub;
t = Timer.periodic(Duration(milliseconds: 500), (timer) {
if (!ctrl.isClosed) {
if (wnd.closed!) {
ctrl.addError(ProtevusHttpException.notAuthenticated(
message:
errorMessage ?? 'Authentication via popup window failed.'));
ctrl.close();
timer.cancel();
sub?.cancel();
}
} else {
timer.cancel();
}
});
sub = window.on[eventName].listen((Event ev) {
var e = ev as CustomEvent;
if (!ctrl.isClosed) {
ctrl.add(e.detail.toString());
t.cancel();
ctrl.close();
sub!.cancel();
}
});
return ctrl.stream;
}
@override
Future logout() {
window.localStorage.remove('token');
return super.logout();
}
}