I am trying to login to my user account which is saved on database and attach a token to that username with 'jwt-token'.
router.post('/users/login', (req, res) => {
User.authenticate()(req.body.username, req.body.password, (err, user) => {
if (err) { console.log(err) }
res.json(user ? jwt.sign({ id: user._id }, process.env.SECRET) : null)
})
})
However when i login to the account, the token is being saved, as [Object][Object] in the local storage via this function here,
// when a user clicks login
const handleLogin = (event) => {
event.preventDefault();
axios.post('/api/users/login', {
username: userState.username,
password: userState.password
}).then(token => {
console.log(token)
setLoginState(true)
localStorage.setItem('username', userState.username)
localStorage.setItem('token', token)
}).catch(err => { console.log(err) })
What is the reason the token is saving as an object? is it not properly being created by my route? or am I not calling it correctly in my .then call? any insight would be appreciated. Still trying to learn as much as i can on backend work and seem to hit problems at every step.