Skip to content

Commit

Permalink
feat(auth, web): migrate web to js_interop to be compatible with WASM (
Browse files Browse the repository at this point in the history
…#12145)

* feat(auth, web): migrate web to js_interop to be compatible with WASM

* continue

* stilll migrating

* interop

* context

* finish interop?

* final

* fix
  • Loading branch information
Lyokone committed Jan 17, 2024
1 parent c917433 commit 8d2df7a
Show file tree
Hide file tree
Showing 10 changed files with 733 additions and 532 deletions.
Expand Up @@ -4,7 +4,7 @@
// found in the LICENSE file.

import 'dart:async';
import 'dart:html';
import 'dart:js_interop';

import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart';
import 'package:firebase_auth_web/src/firebase_auth_web_multi_factor.dart';
Expand All @@ -16,6 +16,7 @@ import 'package:firebase_core_web/firebase_core_web_interop.dart'
as core_interop;
import 'package:flutter/foundation.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:web/web.dart' as web;

import 'src/firebase_auth_web_confirmation_result.dart';
import 'src/firebase_auth_web_recaptcha_verifier_factory.dart';
Expand Down Expand Up @@ -90,9 +91,9 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
ensurePluginInitialized: (firebaseApp) async {
final authDelegate = auth_interop.getAuthInstance(firebaseApp);
// if localhost, and emulator was previously set in localStorage, use it
if (window.location.hostname == 'localhost' && kDebugMode) {
final String? emulatorOrigin =
window.sessionStorage[getOriginName(firebaseApp.name)];
if (web.window.location.hostname == 'localhost' && kDebugMode) {
final String? emulatorOrigin = web.window.sessionStorage
.getItem(getOriginName(firebaseApp.name));

if (emulatorOrigin != null) {
try {
Expand Down Expand Up @@ -327,7 +328,7 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
bool? forceRecaptchaFlow,
}) async {
delegate.settings.appVerificationDisabledForTesting =
appVerificationDisabledForTesting;
appVerificationDisabledForTesting?.toJS;
}

@override
Expand Down Expand Up @@ -464,7 +465,7 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
try {
// Get current session storage value
final String? emulatorOrigin =
window.sessionStorage[getOriginName(delegate.app.name)];
web.window.sessionStorage.getItem(getOriginName(delegate.app.name));

// The generic platform interface is with host and port split to
// centralize logic between android/ios native, but web takes the
Expand All @@ -481,11 +482,12 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
// Save to session storage so that the emulator is used on refresh
// only in debug mode
if (kDebugMode) {
window.sessionStorage[getOriginName(delegate.app.name)] = origin;
web.window.sessionStorage
.setItem(getOriginName(delegate.app.name), origin);
}
} catch (e) {
if (e is auth_interop.AuthError) {
final String code = e.code;
final String code = e.code.toDart;
// this catches Firebase Error from web that occurs after hot reloading & hot restarting
if (code != 'auth/emulator-config-failed') {
throw getFirebaseAuthException(e);
Expand Down
Expand Up @@ -4,10 +4,10 @@
// found in the LICENSE file.

import 'dart:async';
import 'dart:html';

import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart';
import 'package:firebase_auth_web/firebase_auth_web.dart';
import 'package:web/web.dart' as web;

import 'interop/auth.dart' as auth_interop;
import 'utils/web_utils.dart';
Expand Down Expand Up @@ -65,23 +65,24 @@ class RecaptchaVerifierFactoryWeb extends RecaptchaVerifierFactoryPlatform {

if (container == null || container.isEmpty) {
parameters['size'] = 'invisible';
Element? el = window.document.getElementById(_kInvisibleElementId);
web.Element? el =
web.window.document.getElementById(_kInvisibleElementId);

// If an existing element exists, something may have already been rendered.
if (el != null) {
el.remove();
}

window.document.documentElement!.children
.add(DivElement()..id = _kInvisibleElementId);
web.window.document.documentElement!
.appendChild(web.Element()..id = _kInvisibleElementId);

element = _kInvisibleElementId;
} else {
parameters['size'] = convertRecaptchaVerifierSize(size);
parameters['theme'] = convertRecaptchaVerifierTheme(theme);

assert(
window.document.getElementById(container) != null,
web.window.document.getElementById(container) != null,
'An exception was thrown whilst creating a RecaptchaVerifier instance. No DOM element with an ID of $container could be found.',
);

Expand Down Expand Up @@ -129,7 +130,7 @@ class RecaptchaVerifierFactoryWeb extends RecaptchaVerifierFactoryPlatform {
@override
void clear() {
_delegate.clear();
window.document.getElementById(_kInvisibleElementId)?.remove();
web.window.document.getElementById(_kInvisibleElementId)?.remove();
}

@override
Expand Down
Expand Up @@ -4,7 +4,8 @@
// found in the LICENSE file.

import 'dart:async';
import 'dart:js';
import 'dart:js_interop' as js_interop;
import 'dart:js_interop_unsafe';

import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart';
import 'package:firebase_auth_web/src/firebase_auth_web_user_credential.dart';
Expand All @@ -31,12 +32,18 @@ class UserWeb extends UserPlatform {
isEmailVerified: _webUser.emailVerified,
isAnonymous: _webUser.isAnonymous,
creationTimestamp: _webUser.metadata.creationTime != null
? context['Date']
.callMethod('parse', [_webUser.metadata.creationTime])
? (js_interop.globalContext.getProperty('Date'.toJS)!
as js_interop.JSObject)
.callMethod<js_interop.JSNumber>(
'parse'.toJS, _webUser.metadata.creationTime)
.toDartInt
: null,
lastSignInTimestamp: _webUser.metadata.lastSignInTime != null
? context['Date']
.callMethod('parse', [_webUser.metadata.lastSignInTime])
? (js_interop.globalContext.getProperty('Date'.toJS)!
as js_interop.JSObject)
.callMethod<js_interop.JSNumber>(
'parse'.toJS, _webUser.metadata.lastSignInTime)
.toDartInt
: null,
phoneNumber: _webUser.phoneNumber,
photoUrl: _webUser.photoURL,
Expand Down Expand Up @@ -278,16 +285,16 @@ class UserWeb extends UserPlatform {
if (profile.containsKey('displayName') &&
profile.containsKey('photoURL')) {
newProfile = auth_interop.UserProfile(
displayName: profile['displayName'],
photoURL: profile['photoURL'],
displayName: profile['displayName']?.toJS,
photoURL: profile['photoURL']?.toJS,
);
} else if (profile.containsKey('displayName')) {
newProfile = auth_interop.UserProfile(
displayName: profile['displayName'],
displayName: profile['displayName']?.toJS,
);
} else {
newProfile = auth_interop.UserProfile(
photoURL: profile['photoURL'],
photoURL: profile['photoURL']?.toJS,
);
}

Expand Down

0 comments on commit 8d2df7a

Please sign in to comment.