I am using passport-local for user authentication. When I try to get the logged in user it works fine in postman but it shows the error message I set "You need to be logged in first to get the data". My users can log in successfully from vue js but when I try to get the logged in user my error message is shown.
This is my route:
router.get('/jobs', auth ,async(req, res) => { const jobs = await Job.find({}).sort({ createdAt : -1 }) console.log(req.user)//This works in postman but gives an error in vue js res.send(jobs) })
I am using cors and specify origin and set credentials to true.
This is my frontend request:
try{ const res = await axios.get('http://localhost:3000/jobs', { withCredentials : true }) this.jobs = await res.data console.log(this.jobs) // Even though I'm logged in, it displays my error message }catch(error) { if(error.response) { this.message = error.response.data } }
If you use a token, you need to pass it in the request, like this:
View the headers you sent in Postman.
EDIT: Added image