3

I'm trying to use nuxt-auth module, my settings for this module is

  auth: {
    cookie: false,
    plugins: ['~/plugins/api.js'],
    redirect: {
      logout: '/login',
      login: '/',
      home: false
    },
    strategies: {
      local: {
        scheme: 'refresh',
        token: {
          property: 'token',
          maxAge: 3600
        },
        refreshToken: {
          property: 'refresh_token',
          data: 'refresh_token',
          maxAge: 60 * 60 * 24 * 30
        },
        user: {
          property: 'userDetail'
        },
        endpoints: {
          login: { url: 'http://localhost:8085/api/login_check', method: 'post', propertyName: 'token' },
          refresh: { url: 'http://localhost:8085/api/token/refresh', method: 'post', propertyName: 'refresh_token' },
          logout: false,
          user: { url: 'http://localhost:8085/api/user/fetchactive', method: 'get' }
        },
        tokenRequired: true
      }
    }
  }

My "fetchactive" API returns a JSON containing a property "userDetail" which is a string containing the email address, (I also tried to make userDetail an object but with no luck).

e.g.


{"userDetail":{"email":"my@email.test"}}

Nuxt auth keeps telling me that "User Data response does not contain field userDetail".

I also tried to set "property" to false, but Nuxt auth in that cases looks for a field named "false"... I just can't get it to work.

Anyone can help?

sangio90
  • 179
  • 1
  • 3
  • 10

2 Answers2

3

using this configuration in the nuxt.config.js file worked for me

auth: {
      strategies: {
        local: {
          user: {
            property: ''
          },
          //other configs
        }
      }
      //other configs
    }
2

It's happened due to the fact that auth.user endpoint search for 'data' object by default in the response but I does not exist there, as You have Your own response object key, not contained in 'data:{...}

try to add propertyName as in example in auth.user

endpoints: {
      user: { url: 'http://localhost:8085/api/user/fetchactive', method: 'get', propertyName: '' }
    }
  }
devzom
  • 676
  • 1
  • 9
  • 19
  • 1
    I tried your solution but it still does not work. It keeps looking for other property names. I guess it's a bug. I'll go for a manual fetch for user detail and put user: false in the nuxt config... thanks – sangio90 Mar 12 '21 at 08:25
  • Don't do it manually if You use nuxt.auth. Ok hm.... so this is the response from You API: `{"userDetail":{"email":"my@email.test"}} ` ? You can't have 'user' object in ''strategies.local' and in ''strategies.local.endpoints' . Have it in just one place?? Just leave the user object in 'strategies.local.endpoints' and there set propertyName:'' – devzom Mar 12 '21 at 10:25
  • I already tried this but no luck... Were you able to test this and did you get it working? – sangio90 Mar 12 '21 at 15:22
  • Yes, I had same situation in my Nuxt project, and handled it in that way. Also have spent long time to find a solution, but tried many things and found this solution to use empty string in "propertyName" – devzom Mar 16 '21 at 08:49
  • @sangio90 have You handled it? try to set { property: false or '' } see helpful link https://stackoverflow.com/a/55986884/10900851 or https://github.com/nuxt-community/auth-module/issues/398#issuecomment-705085328 – devzom Mar 21 '21 at 15:29
  • Sadly I tried them all, I tried propertyName: false, propertyName: '', property: false, property: '' but still it looks for that "user" field in my response. It does the XHR call properly and it retrieves the user info correctly, but it ignores it and looks for a "user" field. I also tried to change the API to provide a user field in my data, still not working. – sangio90 Mar 30 '21 at 07:14
  • 1
    I finally got it working by debugging the cod. the issue was this: My backend sends a response which seems not to have "data" field, so response.data.anything will always crash because response.data is undefined. What is strange is that both my login and user API do not expose DATA field, I had to manually add it to the user api, in the login API response.data was valid even without setting it manually. – sangio90 Mar 30 '21 at 08:01
  • @sangio90 ahh! great to hear it :) I'm glad that You didn't gave up, good luck with the rest of the project. :) – devzom Apr 02 '21 at 18:47