Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuxt Auth - User Data not set

I try to do a login via nuxt-auth module. As a response I get the token and then the user data is delivered. However, this.$Auth.loggedIn is false and this.$Auth.user is undefined. I have been fighting for 3 days and can not get any further. Hope somebody can help me.

login

await this.$auth.login({
    data: {
        email: this.email,
        password: this.password
    }
}).then(() => {
    this.$router.push('/dashboard')
}).catch(err => {
    this.snackbar.show = true;
})

nuxt.config.js

auth: {
    strategies: {
        local: {
            endpoints: {
                login: {
                    url: '/auth/login',
                    method: 'post',
                    propertyName: 'access_token'
                },
                logout: {
                    url: '/auth/logout',
                    method: 'post'
                },
                user: {
                    url: '/auth/me',
                    method: 'post'
                },
                tokenRequired: true
            }
        }
    }
}

response login

{
"access_token": "xxxxxxxxxxxxx.eyJpc3MiOiJodHRwczpcL1wvYXBpLmFwcHJlexxxxxxxcxXRoXC9sb2dpbiIsImlhdCI6MTUzODI5NTczMywiZXhwIjoxNTM4Mjk5MzMzLCJuYmYiOjE1MzgyOTU3MzMsImp0aSI6ImdtWWVyZTViQjk1cU5BRG8iLCJzdWIiOjIsInBydiI6IjYwODM2NzQ0MzQ4ZDQzMTk4NzE4N2ZjMWM2YzIzMjYxMDcyMWE5ZjAifQ.JhOiwIg7StzZR71aqYyI9rJpPXVclmddzPSIwqCIUN4",
"token_type": "bearer",
"expires_in": 3600
}

response user

{
    "id": 2,
    "name": "Dominik Dummy",
    "email": "[email protected]",
    "created_at": {
        "date": "2018-09-28 09:11:31.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    },
    "updated_at": {
        "date": "2018-09-28 09:11:31.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    },
    "self": "https:\/\/api.apprex.de\/api\/users\/2"
}

enter image description here

like image 241
4ern Avatar asked Sep 30 '18 08:09

4ern


3 Answers

Ok after a long try, I finally solved it. The problem was that auth.fetchUser() requires a property user in the user response, which is not present in my user response. I set the propertyName to false in the nuxt.config.js and now it works

*nuxt.config.js

auth: {
strategies: {
    local: {
        endpoints: {
            login: {
                url: '/auth/login',
                method: 'post',
                propertyName: 'access_token'
            },
            logout: {
                url: '/auth/logout',
                method: 'post'
            },
            user: {
                url: '/auth/me',
                method: 'post',
                propertyName: false // <--- Default "user"
            }
        }
    }
}
}

Currently I am not sure if this is a fault in the module

like image 161
4ern Avatar answered Sep 27 '22 16:09

4ern


Update for auth-next": "^5.0.0"

You will have to set property: false instead of propertyName. So your nuxt.config.js might be as follows:

auth: {
    strategies: {
      local: {
        token: {
          property: 'access_token',
          required: true,
          type: 'Bearer'
        },
        user: {
          property: false, // <--- Default "user"
          autoFetch: true
        },
        endpoints: {
          login: { url: 'api/login', method: 'post' },
          logout: { url: 'api/auth/logout', method: 'post' },
          user: { url: 'api/user', method: 'get' }
        }
      }
    }
  },
like image 25
bar5um Avatar answered Sep 27 '22 18:09

bar5um


You should call the this.$auth.fetchUser() for fills user data and loggedIn in the auth store Docs

like image 38
Hozhabr Avatar answered Sep 27 '22 18:09

Hozhabr