Skip to content

Commit

Permalink
Migrate to web package
Browse files Browse the repository at this point in the history
  • Loading branch information
ThexXTURBOXx committed Feb 17, 2024
1 parent 3aa9370 commit 8eec046
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
2 changes: 1 addition & 1 deletion flutter_web_auth_2/lib/flutter_web_auth_2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 15 additions & 23 deletions flutter_web_auth_2/lib/src/web.dart
Original file line number Diff line number Diff line change
@@ -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).
Expand Down Expand Up @@ -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';

Expand All @@ -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);
Expand Down Expand Up @@ -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<String>();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions flutter_web_auth_2/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8eec046

Please sign in to comment.