From eef0643071198c141d488bdb84c5d2ce43382c82 Mon Sep 17 00:00:00 2001 From: Dan Reynolds Date: Fri, 7 Oct 2022 12:23:24 -0400 Subject: [PATCH] switch to using local storage for passing access token through popups --- lib/src/browser_web_auth.dart | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/src/browser_web_auth.dart b/lib/src/browser_web_auth.dart index b200d0d..ef2e71c 100644 --- a/lib/src/browser_web_auth.dart +++ b/lib/src/browser_web_auth.dart @@ -1,8 +1,11 @@ import 'base_web_auth.dart'; +import 'dart:async'; import 'dart:html' as html; BaseWebAuth createWebAuth() => BrowserWebAuth(); +const _oauthRedirect = 'oauth-redirect'; + class BrowserWebAuth implements BaseWebAuth { @override Future authenticate( @@ -11,16 +14,24 @@ class BrowserWebAuth implements BaseWebAuth { required String redirectUrl, Map? opts}) async { // ignore: unsafe_html - final popupLogin = html.window.open( - url, - 'oauth2_client::authenticateWindow', + html.window.open(url, 'html.window.onMessage', 'menubar=no, status=no, scrollbars=no, menubar=no, width=1000, height=500'); - var messageEvt = await html.window.onMessage - .firstWhere((evt) => evt.origin == Uri.parse(redirectUrl).origin); + final completer = Completer(); + + html.window.onStorage.listen((event) { + if (event.key == _oauthRedirect && !completer.isCompleted) { + html.window.localStorage.remove(_oauthRedirect); + final newValue = event.newValue; + + if (newValue == null) { + throw 'oauth-failed'; + } - popupLogin.close(); + completer.complete(newValue); + } + }); - return messageEvt.data; + return completer.future; } }