Skip to content

Commit

Permalink
fix: move google auth to @react-native-google-signin/google-signin
Browse files Browse the repository at this point in the history
  • Loading branch information
finnmerlett committed Jan 10, 2025
1 parent 0adc25a commit 4caeecf
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
8 changes: 8 additions & 0 deletions app.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
58 changes: 37 additions & 21 deletions src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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'

Expand All @@ -45,7 +46,7 @@ const {
messagingSenderId,
appId,
clientId,
androidClientId,
// androidClientId, // NOTE: now unused?
iosClientId,
} = Constants.expoConfig?.extra as FirebaseSettings

Expand All @@ -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
Expand All @@ -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<ContextType['handleLoginGoogle']>(
() => 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
}
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]":
version "0.76.5"
resolved "https://registry.yarnpkg.com/@react-native/assets-registry/-/assets-registry-0.76.5.tgz#3343338813aa6354df9fec52af50d0b5f7f3d013"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 4caeecf

Please sign in to comment.