'MERN/Axios login only works in opera
I'm creating a forum/imageboard project to learn the MERN stack and while testing my web I've found out that my login system only works in opera. This is what my code looks like:
const [user, setUser] = useState({
name:"",
password:""
});
const handleChange = e => {
const {name, value} = e.target;
setUser ({
...user,
[name]: value
});
}
const setLoginUser = (loginUser) => {
sessionStorage.setItem('activeUser', loginUser);
}
const login = () => {
axios.post("http://localhost:3001/login", user)
.then(res => {alert(res.data.message);
setLoginUser(res.data.user);
navigate("/");
})
}
This is my backend:
app.post("/login",(req, res) => {
const {email, password} = req.body;
UserModel.findOne({email:email}, (err, user) => {
if(user){
if(password === user.password){
res.json({message: "login succesful", user:user});
}else{
res.json({message: "wrong password or email"})
}
}else{
res.json("not registered");
}
});
});
I know sessionStorage is not the best for this but is honestly the only way I managed to do this.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
