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? timer;
late JSFunction listener;
void done() {
timer?.cancel();
window.removeEventListener(eventName, listener);
ctrl.close();
}
// The token, of the event the popup fires on this window.
listener = ((Event e) {
if (ctrl.isClosed) return;
ctrl.add((e as CustomEvent).detail.toString());
done();
}).toJS;
window.addEventListener(eventName, listener);
timer = Timer.periodic(Duration(milliseconds: 500), (_) {
if (ctrl.isClosed) return;
if (wnd != null && wnd.closed) {
ctrl.addError(
AngelHttpException.notAuthenticated(
message: errorMessage ?? 'Authentication via popup window failed.',
),
);
done();
}
});
return ctrl.stream;
}