authenticateViaPopup method
Opens the url
in a new window, and returns a Stream that will fire a JWT on successful authentication.
Implementation
@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<Event>? sub;
Timer.periodic(Duration(milliseconds: 500), (timer) {
if (!ctrl.isClosed) {
if (wnd != null && wnd.closed) {
ctrl.addError(AngelHttpException.notAuthenticated(
message:
errorMessage ?? 'Authentication via popup window failed.'));
ctrl.close();
timer.cancel();
//sub?.cancel();
}
} else {
timer.cancel();
}
});
// TODO: This need to be fixed
EventListener? sub;
window.addEventListener(
eventName,
(e) {
if (!ctrl.isClosed) {
ctrl.add((e as CustomEvent).detail.toString());
//t.cancel();
ctrl.close();
//sub?.cancel();
window.removeEventListener(eventName, sub);
}
}.toJS);
/* With dart:html
sub = window.on[eventName].listen((e) {
if (!ctrl.isClosed) {
ctrl.add((e as CustomEvent).detail.toString());
t.cancel();
ctrl.close();
sub?.cancel();
}
});
*/
return ctrl.stream;
}