Untill recently I used my own register/login implementation in my Nuxt project, where after successful register/login I was able to do this.$axios.setToken(token, 'Bearer')
and it would set the authorization header globally on axios reguests. Now I had to refactor the app and use Nuxt auth module. But now it seems I can't set this header.
This is my auth configuration:
auth: {
strategies: {
local: {
endpoints: {
login: { url: '/auth/local', method: 'post', propertyName: 'jwt' },
logout: false,
user: { url: '/users/me', method: 'get', propertyName: false }
},
}
},
redirect: {
login: '/login',
home: '/home',
user: '/users/me'
},
}
I thought auth was supposed to add this authorization automatically, since it has globalToken
set to true
by default, but it didn't have it. So I tried to specify it explicitly:
tokenRequired: true,
tokenType: 'bearer',
globalToken: true,
autoFetchUser: true
It didn't help. So in the register/login methods I tried to set the token myself, both on axios and $auth modules:
await this.$auth.loginWith('local', {
data
}).then(({data}) => {
this.$apolloHelpers.onLogin(data.jwt)
this.$axios.setToken(data.jwt, 'Bearer')
this.$auth.setToken('local', `Bearer ${data.jwt}`)
...
Also no effect. Though at some point it seems I was able to send only one request successfully, and I saw it did have the Authorization header on the request, but when I switched page and tried to send another request - again it didn't have the header and the request failed with error 403.
So I tried one more thing - in my default layout, in beforeMount() hook, I tried to check if the user is logged in and if he is - set the header:
if (this.$auth.loggedIn) {
this.$axios.setToken(this.$auth.getToken('local'))
}
Again one request was sent successfully, but when switched to another page and tried to send another request - 403.
Instead of $axios.setToken() I tried to set Authorization header:
this.$axios.defaults.headers.common.Authorization = `${this.$auth.getToken('local')}`
And again - one request successful, the other - 403
Why is is happening? How can I set that Authorization header to be present on all requests after register/login or if the user refreshes the page and he is already logged in?