Auth-module: I need the token(s) to be fetched from the header instead of data of the request.

Created on 22 Feb 2018  路  3Comments  路  Source: nuxt-community/auth-module

After digging into the lib, I can see that you fetch the token data from a property name from the axios response data, in my case, I want to fetch multiple tokens from the header not the data. I use devise token auth, and I need to store and send const deviseTokens = ['token-type', 'access-token', 'client', 'uid', 'expiry'], instead of just 1 token and in the Authorization header.

I tried to work it around by using a plugin on my own, which adds an axios interceptor to fetch the headers on every response and set it into every request, everything worked fine except for the login (on app init), in the init section you call fetchUser(), and my interceptors won't be there yet, so it will cause a 401.

I either want: 1. an option to enable/disable the fetchUser() on init, so I can call it myself in the app mounted for example. Or 2. ability to add my interceptors there. Or 3. ability to choose from where to fetch the tokens, and please support the option of having the tokens in multiple properties in the header, not just 1 header field. Or. 4. add support for devise authentication as it is so popular indeed and I see you started to implement login strategies, so it can be one of the strategies.

Thank you so much.

This feature request is available on Nuxt.js community (#c58)
enhancement

Most helpful comment

I second the option to disable fetchUser on init, or at least override it with my own implementation. (i need to do more complex stuff to get the user, don't ask :) )

If we have a way to overwrite fetchUser, and have access to a flag saying "isFirst" or "isInit" or something, ti seems like it would add a ton of flexibility

All 3 comments

I second the option to disable fetchUser on init, or at least override it with my own implementation. (i need to do more complex stuff to get the user, don't ask :) )

If we have a way to overwrite fetchUser, and have access to a flag saying "isFirst" or "isInit" or something, ti seems like it would add a ton of flexibility

I would also like this feature. Since the explosion of popularity in nuxt.js, the rails/nuxt stack is exploding as well, and devise_auth_token is the most popular token solution on rails.

This developer did his own fork to adapt the auth module to devise : https://github.com/WilliamDASILVA/auth-module

Here is my workaround in plugins/axios.js

export default function ({ $axios }) {
  $axios.onRequest(config => {
    config.headers.client = window.localStorage.getItem('client');
    config.headers['access-token'] = window.localStorage.getItem('access-token');
    config.headers.uid = window.localStorage.getItem('uid');
    config.headers['token-type'] = window.localStorage.getItem('token-type');
  })

  $axios.onResponse(response => {
    if (response.headers.client) {
      localStorage.setItem('access-token', response.headers['access-token']);
      localStorage.setItem('client', response.headers.client);
      localStorage.setItem('uid', response.headers.uid);
      localStorage.setItem('token-type', response.headers['token-type']);
    }
  })

Inspired by https://github.com/lynndylanhurley/devise_token_auth/issues/844#issuecomment-339672267

Was this page helpful?
0 / 5 - 0 ratings

Related issues

roosht3 picture roosht3  路  3Comments

javialon26 picture javialon26  路  3Comments

sebmor picture sebmor  路  3Comments

nilskoppelmann picture nilskoppelmann  路  3Comments

DiegoGallegos4 picture DiegoGallegos4  路  3Comments