Skip to content

Commit

Permalink
fix(auth, web): fix an issue that could prevent Recaptcha from being …
Browse files Browse the repository at this point in the history
…properly initialized (#12589)
  • Loading branch information
Lyokone committed Apr 4, 2024
1 parent a81d910 commit 8ce9162
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// found in the LICENSE file.

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

import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart';
import 'package:firebase_auth_web/firebase_auth_web.dart';
Expand Down Expand Up @@ -43,28 +44,28 @@ class RecaptchaVerifierFactoryWeb extends RecaptchaVerifierFactoryPlatform {
RecaptchaVerifierOnExpired? onExpired,
}) : super() {
String element;
Map<String, dynamic> parameters = {};
Map<String, JSAny> parameters = {};

if (onSuccess != null) {
parameters['callback'] = (resp) {
parameters['callback'] = ((JSObject resp) {
onSuccess();
};
}).toJS;
}

if (onExpired != null) {
parameters['expired-callback'] = () {
parameters['expired-callback'] = (() {
onExpired();
};
}).toJS;
}

if (onError != null) {
parameters['error-callback'] = (Object error) {
parameters['error-callback'] = ((JSObject error) {
onError(getFirebaseAuthException(error));
};
}).toJS;
}

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

Expand All @@ -79,15 +80,15 @@ class RecaptchaVerifierFactoryWeb extends RecaptchaVerifierFactoryPlatform {
throw StateError('No document element found');
}

final childElement = web.window.document.createElement('div')
..id = _kInvisibleElementId;
final childElement = web.window.document.createElement('div');
childElement.id = _kInvisibleElementId;

documentElement.appendChild(childElement);

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

assert(
web.window.document.getElementById(container) != null,
Expand Down
24 changes: 16 additions & 8 deletions packages/firebase_auth/firebase_auth_web/lib/src/interop/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -703,13 +703,21 @@ class Auth extends JsObjectWrapper<auth_interop.AuthJsImpl> {
/// For abuse prevention, this method also requires a [ApplicationVerifier].
/// The Firebase Auth SDK includes a reCAPTCHA-based implementation, [RecaptchaVerifier].
Future<ConfirmationResult> signInWithPhoneNumber(
String phoneNumber, ApplicationVerifier applicationVerifier) =>
auth_interop
.signInWithPhoneNumber(
jsObject, phoneNumber.toJS, applicationVerifier.jsObject)
.toDart
.then((value) => ConfirmationResult.fromJsObject(
value! as auth_interop.ConfirmationResultJsImpl));
String phoneNumber,
ApplicationVerifier applicationVerifier,
) async {
final result = await auth_interop
.signInWithPhoneNumber(
jsObject,
phoneNumber.toJS,
applicationVerifier.jsObject,
)
.toDart;

return ConfirmationResult.fromJsObject(
result! as auth_interop.ConfirmationResultJsImpl,
);
}

/// Signs in using a popup-based OAuth authentication flow with the
/// given [provider].
Expand Down Expand Up @@ -1115,7 +1123,7 @@ class RecaptchaVerifier
auth_interop.RecaptchaVerifierJsImpl(
auth.jsObject,
container,
parameters.toJSBox,
parameters.jsify(),
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ class RecaptchaVerifierJsImpl extends ApplicationVerifierJsImpl {
external factory RecaptchaVerifierJsImpl(
AuthJsImpl authExtern,
JSAny containerOrId,
JSObject parameters,
JSAny? parameters,
);
}

Expand Down

0 comments on commit 8ce9162

Please sign in to comment.