From 4caeecf98d876a46842bfe833ee884265e6b2d63 Mon Sep 17 00:00:00 2001 From: Finn Merlett Date: Fri, 10 Jan 2025 11:39:53 +0000 Subject: [PATCH] fix: move google auth to @react-native-google-signin/google-signin --- .eslintrc.js | 6 ++++ app.config.js | 8 +++++ package.json | 2 ++ src/context/AuthContext.tsx | 58 +++++++++++++++++++++++-------------- yarn.lock | 10 +++++++ 5 files changed, 63 insertions(+), 21 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index aca8d4ff..ea0bdb87 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -30,6 +30,12 @@ module.exports = { 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': ['error'], 'react/no-unstable-nested-components': ['warn', { allowAsProps: true }], + 'react-hooks/exhaustive-deps': [ + 'warn', + { + additionalHooks: '(useAsync|useAsyncCallback)', + }, + ], }, settings: { react: { diff --git a/app.config.js b/app.config.js index 322c36b5..433fb8f9 100644 --- a/app.config.js +++ b/app.config.js @@ -82,6 +82,14 @@ export default { playStoreUrl: 'https://play.google.com/store/apps/details?id=com.nearform.optic', }, + plugins: [ + [ + '@react-native-google-signin/google-signin', + { + // iosUrlScheme: 'com.googleusercontent.apps._some_id_here_', // FIXME: fill out + }, + ], + ], runtimeVersion: { policy: 'sdkVersion', }, diff --git a/package.json b/package.json index 6238e8ab..b54319dd 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "@otplib/plugin-base32-enc-dec": "^12.0.1", "@otplib/plugin-crypto-js": "^12.0.1", "@react-native-community/masked-view": "^0.1.11", + "@react-native-google-signin/google-signin": "^13.1.0", "@react-navigation/native": "^7.0.14", "@react-navigation/stack": "^7.1.1", "@types/aes-js": "^3.1.4", @@ -53,6 +54,7 @@ "firebase": "^11.1.0", "global": "^4.4.0", "react": "18.3.1", + "react-async-hook": "^4.0.0", "react-dom": "18.3.1", "react-native": "0.76.5", "react-native-countdown-circle-timer": "^3.2.1", diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx index 71ae0983..856c89d7 100644 --- a/src/context/AuthContext.tsx +++ b/src/context/AuthContext.tsx @@ -1,7 +1,5 @@ import * as AppleAuthentication from 'expo-apple-authentication' -import * as Google from 'expo-auth-session/providers/google' import Constants from 'expo-constants' -import * as WebBrowser from 'expo-web-browser' import { initializeApp } from 'firebase/app' import { User as FirebaseUser, @@ -20,6 +18,9 @@ import React, { useMemo, useState, } from 'react' +import { GoogleSignin } from '@react-native-google-signin/google-signin' +import * as GS from '@react-native-google-signin/google-signin' +import { useAsyncCallback } from 'react-async-hook' import { User } from '../types' @@ -45,7 +46,7 @@ const { messagingSenderId, appId, clientId, - androidClientId, + // androidClientId, // NOTE: now unused? iosClientId, } = Constants.expoConfig?.extra as FirebaseSettings @@ -62,7 +63,10 @@ const firebaseConfig = { const app = initializeApp(firebaseConfig) const auth = getAuth(app) -WebBrowser.maybeCompleteAuthSession() +GoogleSignin.configure({ + webClientId: clientId, + iosClientId, +}) type ContextType = { loading: boolean @@ -80,29 +84,41 @@ type AuthenticationProviderProps = { children: React.ReactNode } -// FIXME: likely not needed any more, remove once auth is working -// eslint-disable-next-line @typescript-eslint/no-unused-vars -const projectNameForProxy = `@${Constants.expoConfig.owner}/${Constants.expoConfig.slug}` - function useGoogleAuth() { - const [, response, promptAsync] = Google.useIdTokenAuthRequest({ - clientId, - androidClientId, - iosClientId, + const { execute: login, result } = useAsyncCallback(async () => { + try { + await GoogleSignin.hasPlayServices() + const response = await GoogleSignin.signIn() + if (GS.isSuccessResponse(response)) { + return { userInfo: response.data } + } else { + console.log('Google sign in cancelled') + } + } catch (error) { + if (GS.isErrorWithCode(error)) { + switch (error.code) { + case GS.statusCodes.IN_PROGRESS: + console.log('Google auth already in progress') + break + case GS.statusCodes.PLAY_SERVICES_NOT_AVAILABLE: + console.log('Google play services not available') + break + default: + console.log('Google sign in error', error) + } + } else { + console.log('An unexpected error occurred,', error) + } + } }) - const login = useCallback( - () => promptAsync(), // FIXME: swap to development build and custom uri scheme - [promptAsync], - ) - useEffect(() => { - if (response?.type === 'success') { - const { id_token } = response.params - const credentials = GoogleAuthProvider.credential(id_token) + if (result) { + const { idToken } = result.userInfo + const credentials = GoogleAuthProvider.credential(idToken) signInWithCredential(auth, credentials) } - }, [response]) + }, [result]) return login } diff --git a/yarn.lock b/yarn.lock index dacc7779..04d2b011 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2605,6 +2605,11 @@ resolved "https://registry.yarnpkg.com/@react-native-community/masked-view/-/masked-view-0.1.11.tgz#2f4c6e10bee0786abff4604e39a37ded6f3980ce" integrity sha512-rQfMIGSR/1r/SyN87+VD8xHHzDYeHaJq6elOSCAD+0iLagXkSI2pfA0LmSXP21uw5i3em7GkkRjfJ8wpqWXZNw== +"@react-native-google-signin/google-signin@^13.1.0": + version "13.1.0" + resolved "https://registry.yarnpkg.com/@react-native-google-signin/google-signin/-/google-signin-13.1.0.tgz#6482b89acf2ac88a6c68bc1304a265d5b7c3bb3a" + integrity sha512-C2/sqb0/s0c+Dwc/mykASZsRuHxGqn7SFrCxCY9D8p8IOQO05haInhCc7lzraJshRixGva5c/4usQZ71HMYSEQ== + "@react-native/assets-registry@0.76.5": version "0.76.5" resolved "https://registry.yarnpkg.com/@react-native/assets-registry/-/assets-registry-0.76.5.tgz#3343338813aa6354df9fec52af50d0b5f7f3d013" @@ -9290,6 +9295,11 @@ rc@~1.2.7: minimist "^1.2.0" strip-json-comments "~2.0.1" +react-async-hook@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/react-async-hook/-/react-async-hook-4.0.0.tgz#1f0467586654e1f33b7433bd98c300a0c5f9b3d0" + integrity sha512-97lgjFkOcHCTYSrsKBpsXg3iVWM0LnzedB749iP76sb3/8Ouu4nHIkCLEOrQWHVYqrYxjF05NN6GHoXWFkB3Kw== + react-devtools-core@^5.3.1: version "5.3.2" resolved "https://registry.yarnpkg.com/react-devtools-core/-/react-devtools-core-5.3.2.tgz#d5df92f8ef2a587986d094ef2c47d84cf4ae46ec"