r/learnreactjs • • Jan 02 '24
Question Separate Email and Password Authentication (React + Firebase)
Here is a simple code in reactjs that requires the user to enter both their email and password to log in to the website:
import { useState } from "react"
import { auth } from "./firebase-config"
import { signInWithEmailAndPassword } from "firebase/auth"
import { useNavigate } from "react-router-dom"
export default function LogIn(){
const [logEmail, setLogEmail] = useState('')
const [logPass, setLogPass] = useState('')
const navigate = useNavigate()
const logIn = (e) => {
e.preventDefault();
signInWithEmailAndPassword(auth, logEmail, logPass)
.then((user) => {
console.log(user)
navigate('/home')
}).catch((err) => {
console.log(err)
})
}
return (
<div>
<form onSubmit={logIn}>
<h3>Log In</h3>
<input
type="email"
placeholder="enter email"
onChange={(e) => {setLogEmail(e.target.value)}}
>
</input>
<input
type="password"
placeholder="Enter Password"
onChange={(e) => {setLogPass(e.target.value)}}
></input>
<button type="submit">Login</button>
</form>
</div>
)
}
Now, this works just fine... but for my website, I want to authenticate the email and the password on a different page. Here's how I require the authentication process to be constructed:
- Display Log In page with only email input
- The user inputs their email and selects submit
- The system verifies if the email exists in the database.
- If the email exists, navigate to the password page where the password authentication takes place.
- Else if the email does not exist, navigate to a page where the user creates an account.
Is there a way to authenticate the email and password separately?
4
Upvotes
View all comments
Show parent comment
1
u/[deleted] Feb 10 '24
Hi, sorry for the late reply. I have changed my project and adjusted it according to new requirements that have the password and email on the same page.
Appreciate the help though :)