reactjs - Firebase signInWithPhoneNumber not returning confirmationResult despite successful reCAPTCHA verification - Stack Over

I'm trying to implement phone authentication using Firebase's signInWithPhoneNumber method. T

I'm trying to implement phone authentication using Firebase's signInWithPhoneNumber method. The reCAPTCHA verification seems to work fine (confirmed via logs), but the confirmationResult is not returned after calling signInWithPhoneNumber hence I'm not getting any OTP message, also with the test phoneNumber I can login successfully. I;ve tried to attach the code related to it at Ideone.

Additional Context:

  1. Using Firebase v9 modular SDK.
  2. reCAPTCHA is implemented using RecaptchaVerifier.
  3. Phone authentication is enabled in the Firebase Console.

Code:

  const signInWithPhone = async (phoneNumber: string): Promise<string> => {
    try {
      const normalizedPhone = phoneNumber.startsWith('+91') ? phoneNumber : `+91${phoneNumber}`;

      if (normalizedPhone === TEST_PHONE) {
        const testUserWithMethods = {
          ...TEST_USER,
          isTestUser: true
        };
        await setupTestUserData();
        localStorage.setItem('testUser', JSON.stringify(testUserWithMethods));
        setUser(testUserWithMethods as unknown as User);
        toast.success('Test account activated!');
        navigate('/profile');
        return 'test-verification-id';
      }


      // Clear any existing state
      clearRecaptcha();

      // Set up new reCAPTCHA
      const verifier = setupRecaptcha();

      // Explicitly render and wait for reCAPTCHA
      try {
        await verifier.render();
      } catch (error) {
        console.error('reCAPTCHA render error:', error);
        clearRecaptcha();
        throw new Error('Please refresh the page and try again');
      }

      // Attempt phone authentication
      const confirmationResult = await signInWithPhoneNumber(auth, normalizedPhone, verifier);

      if (!confirmationResult) {
        throw new Error('Failed to send verification code');
      }

      // Store confirmation result
      confirmationResultRef.current = confirmationResult;

      toast.success('Verification code sent!');
      return confirmationResult.verificationId;
    } catch (error: any) {
      clearRecaptcha();
      console.error('Phone sign-in error:', error);

      // Handle specific error cases
      if (error.code === 'auth/invalid-app-credential' ||
          error.message?.includes('reCAPTCHA')) {
        throw new Error('Please refresh the page and try again');
      }

      const errorMessages = {
        'auth/invalid-phone-number': 'Invalid phone number format',
        'auth/quota-exceeded': 'SMS quota exceeded. Please try again later',
        'auth/captcha-check-failed': 'Security check failed. Please refresh and try again',
        'auth/network-request-failed': 'Network error. Please check your connection',
        'auth/too-many-requests': 'Too many attempts. Please try again later'
      } as const;

      const message = errorMessages[error.code as keyof typeof errorMessages] ||
          error.message ||
          'Failed to send verification code';

      throw new Error(message);
    }
  };

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742347161a4426777.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信