I have this particular jwt.sign function
Backend
const token = jwt.sign({_id: user._id}, process.env.TOKEN_SECRET, {expiresIn: "1m"})
res.header('auth-token', token).json(token)
And this is the JS in my frontend that calls the backend on Login attempt.
Frontend
import axios from "axios";
const login = (email, password) => {
return axios
.post("http://localhost:4000/auth/login", {
email,
password
})
.then((response) => {
if (response.data.accessToken){
localStorage.setItem("user", JSON.stringify(response.data))
}
console.log(response.data)
return response.data
})
}
When i succesfully login, i can see that it gives me the TOKEN (with console.log), but when i return in my Homepage i still have the signup / login buttons instead of the logout button
function App() {
const [currentUser, setCurrentUser] = useState(undefined)
useEffect(() => {
const user = AuthService.getCurrentUser()
if(user) {
setCurrentUser(user)
}else{
console.log("user non ce l'ho")
}
}, [])
const logOut = () => {
AuthService.logout()
}
return (
//previous code
----------------------
{currentUser? (
<div className='navbar-nav ms-auto'>
<li className="nav-item">
<a href="/login" className="nav-link" onClick={logOut}>
Logout
</a>
</li>
</div>
) : (
<div className="navbar-nav ms-auto">
<li className="nav-item">
<Link to={"/login"} className="nav-link">
Login
</Link>
</li>
-----------------------
//post code
)
I have doubts on this jwt.sign({_id: user._id} , what is _id: user._id