Код: Выделить всё
import React, { useState, useEffect } from 'react';
import { View, Text, Pressable } from 'react-native';
import { useNavigation } from 'expo-router';
import { NavigationProp } from '@/lib/types';
import * as Google from 'expo-auth-session/providers/google';
import * as WebBrowser from 'expo-web-browser';
import AsyncStorage from '@react-native-async-storage/async-storage';
WebBrowser.maybeCompleteAuthSession();
const Index = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [userInfo, setUserInfo] = useState(null);
const navigation = useNavigation();
const webClientId = '';
const androidId = '';
const [request, response, promptAsync] = Google.useAuthRequest({
clientId: webClientId,
// androidClientId: androidId,
scopes: ['profile', 'email'],
redirectUri: 'https://auth.expo.io/@username/Frontend',
});
useEffect(() => {
handleSignInWithGoogle();
}, [response]);
const handleSignInWithGoogle = async () => {
if (response?.type === 'success') {
const { accessToken } = response.authentication || {};
await getUserInfo(accessToken ?? '');
} else if (response?.type === 'error') {
console.error('Error during Google sign-in:', response.error);
} else {
console.warn('User dismissed the sign-in dialog or unknown response type');
}
};
const getUserInfo = async (token: any) => {
if (!token) return;
try {
const res = await fetch('https://www.googleapis.com/userinfo/v2/me', {
headers: { Authorization: `Bearer ${token}` },
});
const user = await res.json();
await AsyncStorage.setItem('@user', JSON.stringify(user));
setUserInfo(user);
} catch (error) {
console.error('Failed to fetch user info:', error);
}
};
return (
{JSON.stringify(userInfo)}
promptAsync()}>
Login with Google
);
};
export default Index;
Примечание. Я установил пустые значения androidClientId и webClientId только при вставке сюда, а не в исходный код.
п>
Подробнее здесь: https://stackoverflow.com/questions/791 ... ative-expo