// utils/useGoogleSignIn.js import * as WebBrowser from 'expo-web-browser'; import * as…
- User
// utils/useGoogleSignIn.js
import * as WebBrowser from 'expo-web-browser'; import * as Google from 'expo-auth-session/providers/google'; import { useEffect } from 'react'; import { GoogleAuthProvider, signInWithCredential } from 'firebase/auth'; import { auth } from './firebase'; import Constants from 'expo-constants';
WebBrowser.maybeCompleteAuthSession();
const useGoogleSignIn = () => { const { EXPO_CLIENT_ID, ANDROID_CLIENT_ID, } = Constants.expoConfig.extra;
const [request, response, promptAsync] = Google.useAuthRequest({ expoClientId: EXPO_CLIENT_ID, androidClientId: ANDROID_CLIENT_ID, scopes: ['profile', 'email'], });
useEffect(() => { const authenticate = async () => { if (response?.type === 'success') { const { authentication } = response; const { id_token, access_token } = authentication;
const credential = GoogleAuthProvider.credential(id_token, access_token); try { const userCredential = await signInWithCredential(auth, credential); console.log('User signed in with Google:', userCredential.user); } catch (error) { console.error('Error signing in with Google:', error); } } else if (response?.type === 'error') { console.error('Google Sign-In Error:', response); } }; authenticate();}, [response]);
return { promptAsync }; };
export default useGoogleSignIn; bu dosyayı sadece android client id kullanacak şeklilde güncelle.
- Assistant
Sure! To update your
useGoogleSignInhook to use only theANDROID_CLIENT_ID, you can remove theexpoClientIdfrom theuseAuthRequestconfiguration. This simplifies the authentication process by relying solely on the Android client ID.Here's the updated
useGoogleSignIn.jsfile:// utils/useGoogleSignIn.js import * as WebBrowser from 'expo-web-browser'; import * as Google from 'expo-auth-session/providers/google'; import { useEffect } from 'react'; import { GoogleAuthProvider, signInWithCredential } from 'firebase/auth'; import { auth } from './firebase'; import Constants from 'expo-constants'; WebBrowser.maybeCompleteAuthSession(); const useGoogleSignIn = () => { const { ANDROID_CLIENT_ID } = Constants.expoConfig.extra; const [request, response, promptAsync] = Google.useAuthRequest({ clientId: ANDROID_CLIENT_ID, scopes: ['profile', 'email'], redirectUri: `${Constants.manifest.extra.scheme}:/oauthredirect`, // Ensure redirectUri is properly set }); useEffect(() => { const authenticate = async () => { if (response?.type === 'success') { const { authentication } = response; const { id_token, access_token } = authentication; const credential = GoogleAuthProvider.credential(id_token, access_token); try { const userCredential = await signInWithCredential(auth, credential); console.log('User signed in with Google:', userCredential.user); } catch (error) { console.error('Error signing in with Google:', error); } } else if (response?.type === 'error') { console.error('Google Sign-In Error:', response); } }; authenticate(); }, [response]); return { promptAsync, request }; }; export default useGoogleSignIn;Key Changes Explained
-
Remove
expoClientId:- Before:
const { EXPO_CLIENT_ID, ANDROID_CLIENT_ID } = Constants.expoConfig.extra; - After:
const { ANDROID_CLIENT_ID } = Constants.expoConfig.extra;
- Before:
-
Update
useAuthRequestConfiguration:-
Before:
const [request, response, promptAsync] = Google.useAuthRequest({ expoClientId: EXPO_CLIENT_ID, androidClientId: ANDROID_CLIENT_ID, scopes: ['profile', 'email'], }); -
After:
const [request, response, promptAsync] = Google.useAuthRequest({ clientId: ANDROID_CLIENT_ID, // Use `clientId` for single-client configuration scopes: ['profile', 'email'], redirectUri: `${Constants.manifest.extra.scheme}:/oauthredirect`, // Ensure redirectUri is correctly configured }); -
Notes:
- Use the
clientIdproperty instead of specifying platform-specific client IDs when you want to use a single client ID. - Ensure that the
redirectUriis correctly set to match your Google OAuth configuration. You might need to adjust theschemein your app configuration if it's different.
- Use the
-
-
Optional Enhancements:
- Error Handling:
- Consider providing user feedback in the UI instead of just logging errors to the console.
- Redirect URI Configuration:
- Make sure that the
redirectUriused in theuseAuthRequestmatches the one registered in your Google API console.
- Make sure that the
- Error Handling:
Additional Steps
-
Update
app.jsonorapp.config.js: Ensure that yourapp.jsonorapp.config.jshas the correctexpoconfiguration, especially theextrafield containing theANDROID_CLIENT_IDand potentially theschemeif you're specifying a custom one.{ "expo": { // ... other configurations "extra": { "ANDROID_CLIENT_ID": "your-android-client-id.apps.googleusercontent.com", "scheme": "yourapp" // Ensure this matches the redirectUri } } } -
Google Cloud Console Configuration:
- Verify that the
ANDROID_CLIENT_IDis correctly set up in the Google Cloud Console. - Ensure that the OAuth consent screen is properly configured and that the package name and SHA-1 certificate fingerprint match your Android app.
- Verify that the
-
Testing:
- Test the sign-in flow on an Android device or emulator to ensure that everything works as expected.
- Handle edge cases, such as network failures or user cancellations, to improve the user experience.
By making these changes, your Google Sign-In flow will rely solely on the Android client ID, simplifying the configuration and focusing the authentication process on the Android platform.
-