Home  >  Q&A  >  body text

Implementation of handling redirection after Firebase login in React

<p>I'm trying to implement login functionality using Firebase Authentication in my React app. I am able to successfully authenticate the user with email and password, however I am not able to properly handle redirecting the user to the homepage after logging in. </p> <p>This is my login form component: </p> <pre class="brush:js;toolbar:false;">import { useState } from 'react'; import { useFirebase } from 'react-redux-firebase'; const LoginForm = () => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const firebase = useFirebase(); const handleSubmit = (e) => { e.preventDefault(); firebase.login({ email, password }) } return ( <form onSubmit={handleSubmit}> <input type="email" value={email} onChange={e => setEmail(e.target.value)} placeholder="Email" /> <input type="password" value={password} onChange={e => setPassword(e.target.value)} placeholder="Password" /> <button type="submit">Login</button> </form> ) } export default LoginForm; </pre> <p>How do I handle redirecting the user to the homepage after a successful login? </p>
P粉567112391P粉567112391386 days ago502

reply all(1)I'll reply

  • P粉885562567

    P粉8855625672023-09-03 09:27:27

    If you are using React Router Dom, you can use Redirect in React Router Dom after user authentication. https://reactrouter.com/en/main/fetch/redirect

    For example:

    import { redirect } from "react-router-dom";
     firebase.login({
                email,
                password
            }).then(()=> redirect("/homeppage"));
      }

    reply
    0
  • Cancelreply