Auth-module: [QUESTION] Axios Authorization auto-set

Created on 13 Feb 2018  路  4Comments  路  Source: nuxt-community/auth-module

Should this module be setting my auth-token on all Axios requests. once set? If so, for some reason my token is not set on subsequent requests (It works fine for the login & login_check).

Or do I need to set this myself. If setting it myself, how is best advised for getting access to the token from within a plugin eg this is how I thought, but it does not work:

import axios from 'axios'

export default axios.create({
  headers: {'Authorization': 'Bearer ' + this.$state.auth.token} //--> Cannot read property 'auth' of undefined
  baseURL: 'http://localhost:8000'
})

This question is available on Nuxt.js community (#c50)
need info

Most helpful comment

Which axios instance are you using for your requests? You should be using this.$axios to benefit from the headers set by auth-module.

All 4 comments

@BonnieDoug Could you tell me what is the content of this.$state.auth and this.$auth.state ?

@breakingrobot
In a view compoent this.$state.auth results in:
image

From within a view component this.$auth.state results in:

Template

<template>
  <v-layout>
    <pre>{{ this.$auth.state }}</pre>
  </v-layout>
</template>

Logged in Result:

{
  "user": {
    "id": 61415,
    "contact": 409983,
    "client": {
      "id": 4218,
      "name": "Demonstration"
    },
    "forename": "Bob",
    "surname": "Jones",
    "company": "Bob and Sons Ltd"
  },
  "loggedIn": true
}

No logged in Result:

{
  "user": null,
  "loggedIn": false
}

Which is all as I would expect. I'm just surprised all subsequent requests to the backend don't have the header set. On the login_check & user_check the token is correctly set:

Authorization: Bearer removed_head.eyJyb2xlcyI6WyJST0xFX1VTRVIiXSwidXNlcm5hbWUiOjQzODEwLCJpYXQiOjE1MTg2MDM0NjcsImV4cCI6MTU1NDYwMzQ2N30.removed_sig

Also, all refresh requests (SSR) show the token is set in the Cookie request header, but these do not get set when requesting from the backend (I get 401 errors from my backend)

Which axios instance are you using for your requests? You should be using this.$axios to benefit from the headers set by auth-module.

@paulgv, Massive thank you. I wasn't aware this.$axios existed. All sorted now :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DiegoGallegos4 picture DiegoGallegos4  路  3Comments

aaronhuisinga picture aaronhuisinga  路  3Comments

AhmedAtef07 picture AhmedAtef07  路  3Comments

nikkanetiya picture nikkanetiya  路  3Comments

amjadkhan896 picture amjadkhan896  路  3Comments