Node.js runs successfully in Postman using passport-local but has issues in Vue.js frontend
P粉170858678
P粉170858678 2023-08-30 12:18:47
0
1
280
<p>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. </p> <p>This is my route: </p> <pre class="brush:php;toolbar:false;">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) })</pre> <p>I am using cors and specify origin and set credentials to true. </p> <p>This is my frontend request: </p> <pre class="brush:php;toolbar:false;">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 } }</pre></p>
P粉170858678
P粉170858678

reply all(1)
P粉197639753

If you use a token, you need to pass it in the request, like this:

const config = {
      withCredentials : true,
      headers: { 
        Token: user.value.token 
      },
    };

     try{ 
          const res = await axios.get('http://localhost:3000/jobs', config)

          this.jobs = await res.data
          console.log(this.jobs) // 即使我已经登录,它也会给我错误消息
        }catch(error) {
           if(error.response) {
               this.message = error.response.data
           }
        }

View the headers you sent in Postman.

EDIT: Added image

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!