2024-02-05 23:50:24 -07:00
|
|
|
// Login.tsx
|
2024-02-06 12:34:52 -07:00
|
|
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
|
|
import { FirebaseAuthTypes } from "@react-native-firebase/auth";
|
|
|
|
import React, { useEffect, useState } from "react";
|
2024-02-01 19:43:44 -07:00
|
|
|
import {
|
2024-02-03 20:23:31 -07:00
|
|
|
Button,
|
2024-02-03 20:30:00 -07:00
|
|
|
Keyboard,
|
2024-02-03 20:23:31 -07:00
|
|
|
Text,
|
|
|
|
TextInput,
|
|
|
|
TouchableWithoutFeedback,
|
2024-02-03 20:30:00 -07:00
|
|
|
View,
|
2024-02-06 12:34:52 -07:00
|
|
|
} from "react-native";
|
|
|
|
import {
|
|
|
|
confirmCode,
|
|
|
|
handleSignInWithPhoneNumber,
|
|
|
|
onAuthStateChanged,
|
|
|
|
} from "../auth";
|
|
|
|
import SignOutButton from "../component/buttons/sign-out";
|
|
|
|
import { useAuthHeader } from "../graphql/client";
|
2024-02-01 19:43:44 -07:00
|
|
|
|
2024-02-06 14:02:15 -07:00
|
|
|
export default function Login({ navigation }) {
|
2024-02-06 12:34:52 -07:00
|
|
|
const [phoneNumber, setPhoneNumber] = useState<string>("");
|
|
|
|
const [code, setCode] = useState<string>("");
|
2024-02-05 23:50:24 -07:00
|
|
|
const [user, setUser] = useState<FirebaseAuthTypes.User | null>(null);
|
2024-02-06 12:34:52 -07:00
|
|
|
const [confirm, setConfirm] =
|
|
|
|
useState<FirebaseAuthTypes.ConfirmationResult | null>(null);
|
|
|
|
const { authHeader, setAuthHeader } = useAuthHeader();
|
2024-01-29 15:42:00 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
useEffect(() => {
|
2024-02-06 00:58:39 -07:00
|
|
|
const unsubscribe = onAuthStateChanged(async (user) => {
|
2024-02-06 12:34:52 -07:00
|
|
|
setUser(user);
|
2024-02-06 00:58:39 -07:00
|
|
|
if (user) {
|
2024-02-06 12:34:52 -07:00
|
|
|
const token = await user.getIdToken();
|
2024-02-06 00:58:39 -07:00
|
|
|
if (token) {
|
2024-02-06 12:34:52 -07:00
|
|
|
await AsyncStorage.setItem("token", token);
|
2024-02-06 14:02:15 -07:00
|
|
|
setAuthHeader({ key: "Authorization", value: token });
|
2024-02-06 00:58:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return unsubscribe;
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-02-03 20:23:31 -07:00
|
|
|
}, []);
|
2024-01-29 15:42:00 -07:00
|
|
|
|
2024-02-06 14:02:15 -07:00
|
|
|
console.log(authHeader.value)
|
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
return (
|
|
|
|
<TouchableWithoutFeedback onPress={() => Keyboard.dismiss()}>
|
2024-02-06 12:34:52 -07:00
|
|
|
<View style={{ alignItems: "center" }}>
|
2024-02-03 20:23:31 -07:00
|
|
|
<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
|
2024-02-06 12:34:52 -07:00
|
|
|
title={!confirm ? "Receive code" : "Confirm code"}
|
|
|
|
onPress={() =>
|
|
|
|
!confirm
|
|
|
|
? handleSignInWithPhoneNumber(phoneNumber).then(setConfirm)
|
|
|
|
: confirm && confirmCode(confirm, code)
|
|
|
|
}
|
2024-02-03 20:23:31 -07:00
|
|
|
/>
|
2024-02-06 12:34:52 -07:00
|
|
|
<Text>
|
|
|
|
{authHeader.key}: {authHeader.value}
|
|
|
|
</Text>
|
2024-02-03 20:23:31 -07:00
|
|
|
{user && (
|
|
|
|
<>
|
|
|
|
<Text style={{ marginTop: 10 }}>
|
|
|
|
Display name: {user?.displayName}
|
|
|
|
</Text>
|
|
|
|
<Text>Phone number: {user?.phoneNumber}</Text>
|
2024-02-05 23:50:24 -07:00
|
|
|
|
2024-02-06 12:09:22 -07:00
|
|
|
<SignOutButton />
|
2024-02-06 14:02:15 -07:00
|
|
|
<Button color="orange" title="Go to app" onPress={() => navigation.push("Tabs")} />
|
2024-02-03 20:23:31 -07:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</View>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
);
|
2024-02-01 19:43:44 -07:00
|
|
|
}
|