railbird-gql/screens/login.tsx

86 lines
2.3 KiB
TypeScript

// Login.tsx
import React, { useEffect, useState } from 'react';
import {
Button,
Keyboard,
Text,
TextInput,
TouchableWithoutFeedback,
View,
} from 'react-native';
import { signInWithPhoneNumber, confirmCode, onAuthStateChanged, signOut } from '../auth'; // Adjust the path as necessary
import { FirebaseAuthTypes } from '@react-native-firebase/auth';
import { useAuthHeader } from '../graphql/client';
export default function Login() {
const [phoneNumber, setPhoneNumber] = useState<string>('');
const [code, setCode] = useState<string>('');
const [user, setUser] = useState<FirebaseAuthTypes.User | null>(null);
const [confirm, setConfirm] = useState<FirebaseAuthTypes.ConfirmationResult | null>(null);
const authHeader = useAuthHeader()
useEffect(() => {
const subscriber = onAuthStateChanged(setUser);
return subscriber; // This may need adjustment based on your specific implementation
}, []);
return (
<TouchableWithoutFeedback onPress={() => Keyboard.dismiss()}>
<View style={{ alignItems: 'center' }}>
<TextInput
style={{
width: "50%",
height: 30,
borderWidth: 1,
borderColor: "black",
marginBottom: 20,
}}
placeholder="Phone"
textContentType="telephoneNumber"
keyboardType="phone-pad"
autoCapitalize="none"
value={phoneNumber}
onChangeText={(value) => setPhoneNumber(value)}
/>
{confirm && (
<TextInput
style={{
width: "50%",
height: 30,
borderWidth: 1,
borderColor: "black",
marginBottom: 20,
}}
placeholder="Code"
keyboardType="number-pad"
textContentType="oneTimeCode"
autoCapitalize="none"
value={code}
onChangeText={(value) => setCode(value)}
/>
)}
<Button
title={!confirm ? 'Receive code' : 'Confirm code'}
onPress={() => !confirm ? signInWithPhoneNumber(phoneNumber).then(setConfirm) : confirm && confirmCode(confirm, code)}
/>
<Text>{authHeader.authHeader.key}: {authHeader.authHeader.value}</Text>
{user && (
<>
<Text style={{ marginTop: 10 }}>
Display name: {user?.displayName}
</Text>
<Text>Phone number: {user?.phoneNumber}</Text>
<Button
title={'Sign out'}
onPress={() => signOut()}
/>
</>
)}
</View>
</TouchableWithoutFeedback>
);
}