Search code examples
laravel-5vue.jsaxioslaravel-passport

Laravel passport api/user route is unauthorized


Im using laravel passport for token authentication, and axios for send requests from vuejs frontend. I can successfully login usin oauth/token url and it does return access token, refresh token and other data. But when every time i trying to access api/user route it returns me unauthorized error message. It seems bearer token is not in header but i cannot insert that token to axios header in bootstrap.js it also return error. Can anyone help.


Solution

  • You can do like ,first create an axios instance with token

    const HTTP = axios.create({
      baseURL: `http://baseURL.com/api`,
      headers: {
        Authorization: 'Bearer {token}'
      }
    })
    

    Then you can use that 'HTTP' constant can be in your script to call the request

    created() {
        HTTP.get(`user`)
        .then(response => {
    
        })
        .catch(e => {
          this.errors.push(e)
        })
      }
    

    Refer more here to how to work with axios