0

I'm making front end in React.js and server on python flask and using mongodb as database. When I enter credentials on react js for logging in, I send data to server using axios. python checks the credentials, stores the user data on database and creates a user. I create a session on python server using post request. The problem is this that when I send the data to server using post request, it doesn't make a session even though i wrote the code for that. This is the backend code.

@app.route('/login', methods=['POST', "GET"])
def LoginScreen():
    if request.method == "POST":
        data = request.json
        email = data['email']
        password = data['password']
        user = db.Users.find_one({"email": email, "password": password})
        print(user['_id'])
        if user is None:
            return {"status": "fail"}
        else:
            session['user'] = {"userID": str(user['_id'])}
            print(session['user'])
            return {"status": "success"}
    else:
        session['user'] = "AbdulMunim"
        return 'LoginPage'

and this is the front end code

await axios
  .post("http://localhost:5000/login", {
    email: email,
    password: password,
  })
  .then((res) => {
    console.log("RES", res);
    if (res.data.status === "success") {
      navigate("/");
    } else {
      alert("Invalid Credentials");
    }
  });

When I use POST method to send data from react to flask, it doesn't stores the session but when use the localhost of server side and use login method which is GET, it stores the session.

Abdul Munim
  • 13
  • 1
  • 3

1 Answers1

0

Open chrome's developer tool to check if there's a set-cookie header in flask's response.

If flask works fine, then the problem may happen on the frontend side.

Try this:

axios.post('your_url', data, {withCredentials: true})

This may help you too: Does Axios support Set-Cookie? Is it possible to authenticate through Axios HTTP request?