working with email on create user / sign in -- not getting expected error from firebase

This commit is contained in:
Loewy 2024-02-08 12:32:20 -08:00
parent e2bde3bd01
commit baab800a1c
3 changed files with 43 additions and 14 deletions

View File

@ -30,6 +30,20 @@ export const confirmCode = async (
} }
}; };
export const createOrSignInUser = (email: string, password: string, isSignUp: boolean) => {
console.log('isSignUp', isSignUp);
try {
if (isSignUp) {
auth().createUserWithEmailAndPassword(email, password)
} else {
auth().signInWithEmailAndPassword(email, password)
}
} catch (err) {
}
}
export const onAuthStateChanged = ( export const onAuthStateChanged = (
// TODO: eslint not detecting ts? // TODO: eslint not detecting ts?
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars

View File

@ -7,25 +7,28 @@ import {
Switch, Switch,
Text, Text,
TextInput, TextInput,
TouchableOpacity,
TouchableWithoutFeedback, TouchableWithoutFeedback,
View, View,
} from "react-native"; } from "react-native";
import { confirmCode, handleSignInWithPhoneNumber } from "../auth"; import { confirmCode, handleSignInWithPhoneNumber } from "../auth";
import { createOrSignInUser } from "../auth/firebase-auth";
export default function Login() { export default function Login() {
const [isEmailLogin, setIsEmailLogin] = useState(false); const [isEmailLogin, setIsEmailLogin] = useState(false);
const [phoneNumber, setPhoneNumber] = useState<string>("");
const [code, setCode] = useState<string>("");
const [email, setEmail] = useState<string>("");
const [password, setPassword] = useState<string>("");
const [confirm, setConfirm] =
useState<FirebaseAuthTypes.ConfirmationResult | null>(null);
const toggleSwitch = () => setIsEmailLogin((previousState) => !previousState); const toggleSwitch = () => setIsEmailLogin((previousState) => !previousState);
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars // Phone number
const handleSignInWithEmail = (email: string, password: string) => const [phoneNumber, setPhoneNumber] = useState<string>("");
console.log("signingInWithEmail"); const [code, setCode] = useState<string>("");
const [confirm, setConfirm] =
useState<FirebaseAuthTypes.ConfirmationResult | null>(null);
// Email
const [isSignUp, setIsSignUp] = useState<boolean>(false)
const [email, setEmail] = useState<string>("");
const [password, setPassword] = useState<string>("");
const toggleSignUp = () => setIsSignUp(!isSignUp);
return ( return (
<TouchableWithoutFeedback onPress={() => Keyboard.dismiss()}> <TouchableWithoutFeedback onPress={() => Keyboard.dismiss()}>
@ -94,11 +97,16 @@ export default function Login() {
value={password} value={password}
onChangeText={setPassword} onChangeText={setPassword}
/> />
<TouchableOpacity onPress={toggleSignUp}>
<Text style={styles.linkText}>
{isSignUp ? 'Already have an account? Sign In' : "Don't have an account? Sign Up"}
</Text>
</TouchableOpacity>
<View style={styles.buttonContainer}> <View style={styles.buttonContainer}>
<Button <Button
title="Sign in" title={isSignUp ? 'SignUp' : 'Sign in'}
onPress={() => handleSignInWithEmail(email, password)} onPress={() => createOrSignInUser(email, password, isSignUp)}
/> />
</View> </View>
</> </>
@ -130,6 +138,12 @@ const styles = StyleSheet.create({
alignItems: "center", alignItems: "center",
marginBottom: 20, marginBottom: 20,
}, },
linkText: {
fontSize: 16,
color: '#007AFF',
paddingVertical: 10,
textAlign: 'center',
},
buttonContainer: { buttonContainer: {
width: "80%", width: "80%",
borderRadius: 25, borderRadius: 25,

View File

@ -7,14 +7,15 @@ import { useAuth } from "../context";
// Sign out button only functional when NOT using dev env // Sign out button only functional when NOT using dev env
export default function ProfileScreen() { export default function ProfileScreen() {
const { user } = useAuth(); const { user } = useAuth();
console.log(user)
return ( return (
<View style={styles.container}> <View style={styles.container}>
{user && ( {user && (
<> <>
<View style={styles.userInfo}> <View style={styles.userInfo}>
<Text>Display name: {user?.displayName}</Text> <Text>Display name: {user?.displayName ?? 'No username set'}</Text>
<Text>Phone number: {user?.phoneNumber}</Text> <Text>Phone number/Email: {user?.phoneNumber ?? user?.email}</Text>
</View> </View>
<View style={styles.signOutButton}> <View style={styles.signOutButton}>
<SignOutButton /> <SignOutButton />