„expo-auth-session“ und „expo-auth-session/providers/google“ zu implementieren.
Umgebung:
- React Native (Expo Router)
- Läuft in Expo Go auf iOS
- Google OAuth-Client-IDs, die in Google erstellt wurden Cloud:
- Webanwendung
- iOS
- Android
Code: Select all
import { AntDesign } from '@expo/vector-icons';Code: Select all
import AsyncStorage from '@react-native-async-storage/async-storage';Code: Select all
import * as Google from 'expo-auth-session/providers/google';Code: Select all
import { useRouter } from 'expo-router';Code: Select all
import * as WebBrowser from 'expo-web-browser';Code: Select all
import React, { useCallback, useEffect, useState } from 'react';Code: Select all
import { Alert, Button, Pressable, Text, View } from 'react-native';Code: Select all
import IconFacebook from '../../../icons/IconFacebook';Code: Select all
import IconGoogle from '../../../icons/IconGoogle';Code: Select all
WebBrowser.maybeCompleteAuthSession();Code: Select all
export default function LoginScreen() {Code: Select all
const router = useRouter();Code: Select all
const [, setUserInfo] = useState(null);Code: Select all
const redirectUri = 'https://auth.expo.io/@trieuman2705/bkoo';Code: Select all
const [request, response, promptAsync] = Google.useAuthRequest({Code: Select all
expoClientId:Code: Select all
'8263....apps.googleusercontent.com',Code: Select all
iosClientId:Code: Select all
'8263...apps.googleusercontent.com',Code: Select all
androidClientId:Code: Select all
'8263332....apps.googleusercontent.com',Code: Select all
webClientId:Code: Select all
'8263332....apps.googleusercontent.com',Code: Select all
scopes: ['profile', 'email'],Code: Select all
redirectUri,Code: Select all
});Code: Select all
useEffect(() => {Code: Select all
console.log('Google redirectUri =', request?.redirectUri);Code: Select all
}, [request]);Code: Select all
// ...fetchUserInfo + UI omitted for brevityCode: Select all
}Code: Select all
'8263332....apps.googleusercontent.com'Code: Select all
https://auth.expo.io/@trieuman2705/bkooWenn ich jedoch den Anmeldefluss starte und die generierte Anfrage protokolliere, sehe ich Folgendes:
Code: Select all
OAuth Request: {Code: Select all
redirectUri: "https://auth.expo.io/@trieuman2705/bkoo",Code: Select all
request: {Code: Select all
clientId: "826333210617-dogrjmu5121isqo1gdnblogr23j6qh6b.apps.googleusercontent.com", //
Mobile version