|
| 1 | +import { useEffect } from 'react'; |
| 2 | +import { View, StyleSheet } from 'react-native'; |
| 3 | +import { GoogleSignin, GoogleSigninButton } from '@react-native-google-signin/google-signin'; |
| 4 | +import Constants from 'expo-constants'; |
| 5 | +import { useAuth } from '@/hooks/useAuth'; |
| 6 | + |
| 7 | +export default function LoginScreen() { |
| 8 | + const { setUser } = useAuth(); |
| 9 | + |
| 10 | + useEffect(() => { |
| 11 | + GoogleSignin.configure({ |
| 12 | + webClientId: Constants.expoConfig?.extra?.googleWebClientId, |
| 13 | + }); |
| 14 | + }, []); |
| 15 | + |
| 16 | + const signIn = async () => { |
| 17 | + try { |
| 18 | + await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true }); |
| 19 | + const result = await GoogleSignin.signIn(); |
| 20 | + if (result.type === 'success') { |
| 21 | + setUser({ idToken: result.data.idToken ?? '', user: result.data.user }); |
| 22 | + fetch('https://new.codebuilder.org/api/auth/google', { |
| 23 | + method: 'POST', |
| 24 | + headers: { 'Content-Type': 'application/json' }, |
| 25 | + body: JSON.stringify({ idToken: result.data.idToken }), |
| 26 | + }).catch((e) => console.error('Auth callback error:', e)); |
| 27 | + } |
| 28 | + } catch (e) { |
| 29 | + console.error('Google sign in error:', e); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + return ( |
| 34 | + <View style={styles.container}> |
| 35 | + <GoogleSigninButton |
| 36 | + onPress={signIn} |
| 37 | + size={GoogleSigninButton.Size.Wide} |
| 38 | + color={GoogleSigninButton.Color.Dark} |
| 39 | + /> |
| 40 | + </View> |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +const styles = StyleSheet.create({ |
| 45 | + container: { flex: 1, justifyContent: 'center', alignItems: 'center' }, |
| 46 | +}); |
0 commit comments