Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions lib/src/browser_web_auth.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// ignore_for_file: avoid_web_libraries_in_flutter

import 'dart:html' as html;

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<String> authenticate(
Expand All @@ -14,16 +14,24 @@ class BrowserWebAuth implements BaseWebAuth {
required String redirectUrl,
Map<String, dynamic>? 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<String>();

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;
}
}