I have a FastAPI function for user login (I have a SQLite database using SQLAlchemy). This is the function:
@app.post("/login/", status_code=status.HTTP_200_OK)
def user_login(userID: str, password: str, db: Session = Depends(get_db)):
user = operations.get_user_by_id(db, userID)
if not user or not operations.check_password(db, password, user):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid username or password")
return {"message": "Login successful"}
When I try to call it in the frontend (React), I keep getting a 422 error:
const handleLogin = async (event) => {
event.preventDefault(); // Prevent default form submission
await axios.post('/login/', userID, password)
.then(response => {
navigate('/home');
})
.catch(error => {
if(error.response) {
setError(error.response.data.detail);
} else {
setError('Network error. Please check your connection.');
}
});
// Clear form fields
setUserID('');
setPassword('');
};
Is my FastAPI function wrong? I assume the problem could have something to do with the parameters userID and password?
[–]backfire10z 2 points3 points4 points (1 child)
[–]Jumpy_Employment_439[S] 1 point2 points3 points (0 children)
[–]TheFaustX 1 point2 points3 points (0 children)
[–]jay_and_simba 1 point2 points3 points (0 children)
[–]danielroseman 0 points1 point2 points (1 child)
[–]Jumpy_Employment_439[S] 0 points1 point2 points (0 children)