All files / apps/host/src/hooks useGoogleLogin.ts

93.33% Statements 56/60
72.72% Branches 8/11
75% Functions 6/8
94.73% Lines 54/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115                                3x 2x     2x     3x 12x 12x 12x   12x 4x   4x 4x 4x 4x   4x 4x     4x     3x 6x 3x 3x         1x     2x 2x   2x 2x   2x 2x 2x   2x 2x   2x 2x           2x 2x 2x       12x 2x 2x   2x 2x   2x 2x 2x 2x 2x               2x 1x   1x 1x 1x   1x 1x 1x       12x    
import { useState } from 'react';
import {
  getAuth,
  GoogleAuthProvider,
  signInWithCredential,
  signOut,
} from '@react-native-firebase/auth';
import { GoogleSignin } from '@react-native-google-signin/google-signin';
 
import { FIREBASE_ERROR_CODES } from '@repo/constants/error';
import { AUTH_ERROR_MESSAGES } from '@repo/constants/messages';
 
import { deleteNotificationToken, getDeviceInfo } from '@/services/notification/notificationApi';
import { queryClient } from '@/services/queryClient';
import { sentryService } from '@/services/sentryService';
 
const handleSigninError = (error: unknown) => {
  Iif ((error as { code: string }).code === FIREBASE_ERROR_CODES.AUTH_DISABLED) {
    return new Error(AUTH_ERROR_MESSAGES.USER_DISABLED);
  }
  return error as Error;
};
 
export const useGoogleLogin = () => {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<Error | null>(null);
  const [isSuccess, setIsSuccess] = useState(false);
 
  const login = async () => {
    const auth = getAuth();
 
    try {
      setLoading(true);
      setError(null);
      setIsSuccess(false);
 
      await GoogleSignin.signOut();
      await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
 
      // Force user to pick account
      const signInResult = await GoogleSignin.signIn();
 
      // Validate email domain, bypassing for explicitly whitelisted test/review accounts
      const email = signInResult.data?.user.email;
      const allowedDomains = process.env.ALLOWED_EMAIL_DOMAINS?.split(',').map(d => d.trim());
      const allowedTestEmails = process.env.ALLOWED_TEST_EMAILS?.split(',').map(e => e.trim());
      if (
        email &&
        !allowedTestEmails?.includes(email) &&
        !allowedDomains?.includes(email.split('@')[1])
      ) {
        throw new Error(AUTH_ERROR_MESSAGES.NOT_ALLOWED_EMAIL_DOMAIN);
      }
 
      const idToken = signInResult.data?.idToken;
      Iif (!idToken) throw new Error(AUTH_ERROR_MESSAGES.COMMON);
 
      const googleCredential = GoogleAuthProvider.credential(idToken);
      const userCredential = await signInWithCredential(auth, googleCredential);
 
      setLoading(false);
      setIsSuccess(true);
      return userCredential;
    } catch (e) {
      setLoading(false);
      setIsSuccess(false);
 
      try {
        await signOut(auth).catch(() => {});
        await GoogleSignin.signOut().catch(() => {});
      } catch {
        // Ignore cleanup errors
      }
 
      const errorFormatted = handleSigninError(e);
      setError(errorFormatted);
      throw errorFormatted;
    }
  };
 
  const logout = async () => {
    const auth = getAuth();
    const currentUser = auth.currentUser;
 
    setLoading(true);
    setIsSuccess(false);
 
    try {
      Eif (currentUser) {
        try {
          const deviceInfo = getDeviceInfo();
          await deleteNotificationToken(currentUser.uid, deviceInfo.deviceId);
        } catch (deleteError) {
          sentryService.captureException(deleteError, {
            tags: { component: 'notification', action: 'delete_token_on_logout' },
          });
        }
      }
 
      await signOut(auth);
      await GoogleSignin.signOut();
 
      queryClient.clear();
      sentryService.clearAuthenticatedUser();
      setLoading(false);
    } catch (e) {
      setError(e as Error);
      setLoading(false);
      throw e;
    }
  };
 
  return { login, logout, loading, error, isSuccess };
};