2024-02-03 20:30:00 -07:00
|
|
|
import auth, { 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
|
|
|
Alert,
|
|
|
|
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-01 19:43:44 -07:00
|
|
|
} from "react-native";
|
2024-01-29 15:42:00 -07:00
|
|
|
|
2024-01-29 15:50:31 -07:00
|
|
|
// This code is beginning of Auth Implementation - actual implementation will differ and involve more UI
|
|
|
|
// Does not have a restart or proper handling of code confirmation, should only be used for obtaining token/testing
|
|
|
|
// Currently working for Android builds, iOS has open issue #56
|
2024-02-01 19:43:44 -07:00
|
|
|
|
2024-01-29 15:42:00 -07:00
|
|
|
export default function Login() {
|
2024-02-03 20:23:31 -07:00
|
|
|
const [phoneNumber, setPhoneNumber] = useState<string>("");
|
|
|
|
const [code, setCode] = useState<string>("");
|
2024-01-29 15:42:00 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
const [user, setUser] = useState(null);
|
|
|
|
const [confirm, setConfirm] =
|
|
|
|
useState<FirebaseAuthTypes.ConfirmationResult | null>(null);
|
2024-01-29 15:42:00 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
async function onAuthStateChanged(user: any) {
|
|
|
|
setUser(user);
|
|
|
|
if (user) {
|
|
|
|
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
|
|
|
|
const token = await auth().currentUser?.getIdToken();
|
|
|
|
// To debug/check token & user return, use these logs
|
|
|
|
// console.log(token) // token log
|
|
|
|
// console.log(user) // user log
|
|
|
|
}
|
|
|
|
}
|
2024-01-29 15:42:00 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
async function signInWithPhoneNumber(phoneNumber: string) {
|
|
|
|
if (!phoneNumber) {
|
|
|
|
return Alert.alert(
|
|
|
|
"Please enter a valid phone number with a country code",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const confirmation = await auth().signInWithPhoneNumber(phoneNumber);
|
|
|
|
setConfirm(confirmation);
|
|
|
|
} catch (err) {
|
|
|
|
// TODO: implement more robust error handling by parsing err message
|
|
|
|
console.warn(err);
|
|
|
|
Alert.alert(
|
|
|
|
"There was an error. Make sure you are using a country code (ex: +1 for US)",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-01-29 15:42:00 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
async function confirmCode() {
|
|
|
|
try {
|
|
|
|
await confirm?.confirm(code);
|
|
|
|
} catch {
|
|
|
|
Alert.alert("Invalid code, please try again.");
|
|
|
|
}
|
|
|
|
}
|
2024-01-29 15:42:00 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
useEffect(() => {
|
|
|
|
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
|
|
|
|
return subscriber;
|
|
|
|
}, []);
|
2024-01-29 15:42:00 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
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) : confirmCode()
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
{user && (
|
|
|
|
<>
|
|
|
|
<Text style={{ marginTop: 10 }}>
|
|
|
|
Display name: {user?.displayName}
|
|
|
|
</Text>
|
|
|
|
<Text>Phone number: {user?.phoneNumber}</Text>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</View>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
);
|
2024-02-01 19:43:44 -07:00
|
|
|
}
|