Nuxt Auth Module - how to get a user by id/username Nuxt Auth Module - how to get a user by id/username vue.js vue.js

Nuxt Auth Module - how to get a user by id/username


Faced to this problem, too.

My solution:

  1. Set user property of auth endpoint to false
auth: {     strategies: {      local: {        endpoints: {          // login api          login: {              url: '/api/v1/users/login',            method: 'post',            propertyName: 'token'          },          // logout api          logout: {            url: '/api/v1/users/logout',            method: 'post'          },          user: false // setting user fetch api to false        },        redirect: {          login: '/login',          logout: '/login',          callback: '/login',          home: '/'        },      }    }  },
  1. After loginWith() You can use function setUniversal(key, value, isJson) to save fetched user and get it with function getUniversal(key)
  async login(user) {    await this.$auth.loginWith('local', {      data: user    }).then(res => {      let user = res.data.data.user // getting user (yours can be different)      this.$auth.$storage.setUniversal('user', user, true) // setting user in Vuex, cookies and localstorage      user = this.$auth.$storage.getUniversal('user') // getting user (you can use it anywhere in your app)      console.log(user) // checking user      this.$router.push('/') // redirecting after login    }).catch(err => {      console.log(err.response)    })  }
  1. That's all, you have your user in vuex, cookies, and localstorage you can get it in computed like this:
computed: {  user() {    return this.$auth.$storage.getUniversal('user');  }}

P.S: to logout, use logout() function, and in the callback use this.$auth.$storage.removeUniversal('user') to remove it from everywhere