From 8eec0460ba0924b997240251b852595f45d64667 Mon Sep 17 00:00:00 2001 From: Nico Mexis Date: Sat, 17 Feb 2024 20:20:44 +0100 Subject: [PATCH] Migrate to web package --- .../lib/flutter_web_auth_2.dart | 2 +- flutter_web_auth_2/lib/src/web.dart | 38 ++++++++----------- flutter_web_auth_2/pubspec.yaml | 1 + 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/flutter_web_auth_2/lib/flutter_web_auth_2.dart b/flutter_web_auth_2/lib/flutter_web_auth_2.dart index 32604ac..91e40be 100644 --- a/flutter_web_auth_2/lib/flutter_web_auth_2.dart +++ b/flutter_web_auth_2/lib/flutter_web_auth_2.dart @@ -9,7 +9,7 @@ import 'package:flutter_web_auth_2_platform_interface/flutter_web_auth_2_platfor export 'src/options.dart'; export 'src/unsupported.dart' if (dart.library.io) 'src/linows.dart' - if (dart.library.html) 'src/web.dart'; + if (dart.library.js_interop) 'src/web.dart'; class _OnAppLifecycleResumeObserver extends WidgetsBindingObserver { _OnAppLifecycleResumeObserver(this.onResumed); diff --git a/flutter_web_auth_2/lib/src/web.dart b/flutter_web_auth_2/lib/src/web.dart index 0fc4d78..04d90c4 100644 --- a/flutter_web_auth_2/lib/src/web.dart +++ b/flutter_web_auth_2/lib/src/web.dart @@ -1,12 +1,13 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:html'; +import 'dart:js_interop'; import 'package:flutter/services.dart'; import 'package:flutter_web_auth_2/src/options.dart'; import 'package:flutter_web_auth_2_platform_interface/flutter_web_auth_2_platform_interface.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; import 'package:url_launcher/url_launcher.dart'; +import 'package:web/web.dart'; /// Implements the plugin interface using iframes (for silent authentication) /// and through an event listener system (for usual authentication). @@ -51,8 +52,7 @@ class FlutterWebAuth2WebPlugin extends FlutterWebAuth2Platform { if (parsedOptions.silentAuth) { // Not in our hands - developers need to check sanity of URL themselves... - final authIframe = IFrameElement() - // ignore: unsafe_html + final authIframe = HTMLIFrameElement() ..src = url ..style.display = 'none'; @@ -65,7 +65,9 @@ class FlutterWebAuth2WebPlugin extends FlutterWebAuth2Platform { messageSubscription = window.onMessage.listen((messageEvent) { if (messageEvent.origin == (parsedOptions.debugOrigin ?? Uri.base.origin)) { - final authMessage = messageEvent.data['flutter-web-auth-2']; + final data = messageEvent.data.dartify(); + final authMessage = + data != null && data is Map ? data['flutter-web-auth-2'] : null; if (authMessage is String) { authIframe.remove(); completer.complete(authMessage); @@ -98,7 +100,7 @@ class FlutterWebAuth2WebPlugin extends FlutterWebAuth2Platform { // Remove the old record if it exists const storageKey = 'flutter-web-auth-2'; - window.localStorage.remove(storageKey); + window.localStorage.removeItem(storageKey); Timer? lsTimer; StreamSubscription? messageSubscription; final completer = Completer(); @@ -107,23 +109,12 @@ class FlutterWebAuth2WebPlugin extends FlutterWebAuth2Platform { // If it exists, return it. If not, check the timeout. // If the timeout has passed, throw an exception. lsTimer = Timer.periodic(const Duration(seconds: 1), (timer) { - if (window.localStorage.containsKey(storageKey)) { - final flutterWebAuthMessage = window.localStorage[storageKey]; - if (flutterWebAuthMessage is String) { - completer.complete(flutterWebAuthMessage); - window.localStorage.remove(storageKey); - messageSubscription?.cancel(); - timer.cancel(); - } else { - messageSubscription?.cancel(); - timer.cancel(); - completer.completeError( - PlatformException( - code: 'error', - message: 'Callback value is not a string', - ), - ); - } + final flutterWebAuthMessage = window.localStorage.getItem(storageKey); + if (flutterWebAuthMessage != null) { + completer.complete(flutterWebAuthMessage); + window.localStorage.removeItem(storageKey); + messageSubscription?.cancel(); + timer.cancel(); } else if (timer.tick >= parsedOptions.timeout) { messageSubscription?.cancel(); timer.cancel(); @@ -141,8 +132,9 @@ class FlutterWebAuth2WebPlugin extends FlutterWebAuth2Platform { (messageEvent) { if (messageEvent.origin == (parsedOptions.debugOrigin ?? Uri.base.origin)) { + final data = messageEvent.data.dartify(); final flutterWebAuthMessage = - messageEvent.data['flutter-web-auth-2']; + data != null && data is Map ? data['flutter-web-auth-2'] : null; if (flutterWebAuthMessage is String) { lsTimer?.cancel(); messageSubscription?.cancel(); diff --git a/flutter_web_auth_2/pubspec.yaml b/flutter_web_auth_2/pubspec.yaml index 83198d0..ead788b 100644 --- a/flutter_web_auth_2/pubspec.yaml +++ b/flutter_web_auth_2/pubspec.yaml @@ -32,6 +32,7 @@ dependencies: sdk: flutter path_provider: ^2.1.2 url_launcher: ^6.1.6 + web: ^0.5.0 window_to_front: ^0.0.3 dev_dependencies: