Auth-module: Access auth-module state in other plugin

Created on 15 Jan 2018  路  1Comment  路  Source: nuxt-community/auth-module

I have a plugin and I want to access logged in user. Something like this (example):

import Vue from 'vue'
import store from '~/store'

let username = store.state.user.username

This question is available on Nuxt.js community (#c26)
question

Most helpful comment

Hi @pedramk. Using latest version (4.0.0) you can use plugins option to extend or access auth.

nuxt.config.js

{
  modules: {
    '@nuxtjs/auth'
  },
  auth: {
     plugins: [ '~/plugins/auth.js' ]
  }
}

plugins/auth.js:

export default function ({ app }) {
  if (!app.$auth.loggedIn) {
    return
  }

  const username = app.$auth.user.username
}

>All comments

Hi @pedramk. Using latest version (4.0.0) you can use plugins option to extend or access auth.

nuxt.config.js

{
  modules: {
    '@nuxtjs/auth'
  },
  auth: {
     plugins: [ '~/plugins/auth.js' ]
  }
}

plugins/auth.js:

export default function ({ app }) {
  if (!app.$auth.loggedIn) {
    return
  }

  const username = app.$auth.user.username
}
Was this page helpful?
0 / 5 - 0 ratings