Home  >  Q&A  >  body text

Node.js runs successfully in Postman using passport-local but has issues in Vue.js frontend

<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粉170858678P粉170858678416 days ago383

reply all(1)I'll reply

  • P粉197639753

    P粉1976397532023-08-31 11:05:53

    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

    reply
    0
  • Cancelreply