1

I'm currently working on a personal project and I'm stuck at the moment with the login page. I mention that I use Firebase Cloud functions and axios to post / get requests.

So, this is the cloud function for the login:

app.post('/login', (req,res) => {
    const user = {
        email: req.body.email,
        password: req.body.password,
    };

    let errors = {};

    if(isEmpty(user.email)) errors.email = 'Must not be empty !';
    if(isEmpty(user.password)) errors.email = 'Must not be empty !';
    
    if(Object.keys(errors).length > 0) return res.status(400).json(errors);

    firebase.auth().signInWithEmailAndPassword(user.email,user.password)
            .then(() => {
                return res.json(user.email);
            })
            .catch(err => {
                console.error(err);
                res.status(500).json({error: err.code});
            })

})

which works fine since I could send data via Postman.

There is the code from the Login.js :

    const [email,setEmail] = useState("");
    const [password,setPassword] = useState("");

    const handleSubmit = (e) => {
        e.preventDefault();
        axios
            .post('/login', {
                email: email,
                password: password,
            })
            .then((res) => {
                console.log(res.data);
                this.props.history.push('/');
            })
            .catch((err) => {
                console.log(err);
            })
    }

And this is the form:

<input value={email} onChange={(e) => setEmail(e.target.value)} id="email" name="password" className="login__input" type="email" placeholder="Email"/>

<input value={password} onChange={(e) => setPassword(e.target.value)} id="password" name="password" className="login__input" type="password" placeholder="Password"/>

<button onClick={handleSubmit} className="login__button" type="submit">Connect</button

And I also added the proxy in the package.json:

"proxy" : "http://localhost:5000/XXXXXXXXXX/europe-west1/api"

In this state, I get a 500 Internal server error status. I tried many different options, that give me Access-Control-Allow-origin error even though I tried to fix it, it seems that it doesn't work either way.

EDIT:

console:

> functions: Beginning execution of "api"
> >  [t [Error]: The email address is badly formatted.] {
> >    code: 'auth/invalid-email',
> >    a: null
> >  } i  functions: Finished "api" in ~1s i  functions: Beginning execution of "api" i  functions: Finished "api" in ~1s i  functions:
> Beginning execution of "api"
> >  [t [Error]: The password is invalid or the user does not have a password.] {
> >    code: 'auth/wrong-password',
> >    a: null
> >  } i  functions: Finished "api" in ~1s i  functions: Beginning execution of "api"
> >  [t [Error]: The password is invalid or the user does not have a password.] {
> >    code: 'auth/wrong-password',
> >    a: null
> >  } i  functions: Finished "api" in ~1s i  functions: Beginning execution of "api"
> >  [t [Error]: The password is invalid or the user does not have a password.] {
> >    code: 'auth/wrong-password',
> >    a: null
> >  } i  functions: Finished "api" in ~1s i  functions: Beginning execution of "api" i  functions: Finished "api" in ~1s

Thank you.

darksz
  • 115
  • 2
  • 12
  • There is an error in your Cloud Function code. Please edit the question to show what you see in the logs in the console. – Doug Stevenson Sep 03 '20 at 00:03
  • Done it @DougStevenson – darksz Sep 03 '20 at 00:10
  • That's not your cloud functions console log. That's your web browser console log. – Doug Stevenson Sep 03 '20 at 00:37
  • @DougStevenson I updated it, i tried different inputs, and it seems like it's working ?? but it doesnt redirect me to the front page – darksz Sep 03 '20 at 00:51
  • I've figured it out. There was also an error about this.props.history.push but i solved it thanks to this answer: https://stackoverflow.com/a/58536772/12169097 ( in case somebody gets the same "bug" as I did ). Thank you anyways. – darksz Sep 03 '20 at 01:15
  • @darksz great to know that you found an answer in the community that fixed the issue you were facing. Do consider adding the comment above to an answer and elaborating on what your did using the answer you mentioned in order to fix the issue. That way other community members may refer to your answer if they go through the same issue and also this will help increase your reputation. – Ralemos Sep 03 '20 at 12:43

0 Answers0