Auth-module: this.$auth.loggedIn is always FALSE

Created on 14 Jan 2021  路  8Comments  路  Source: nuxt-community/auth-module

my this.$auth.loggedIn is always FALSE, please help!!!
I am logging in, getting a valid jwt token and getting user details back in the response!

here is my NUXT auth version:
"@nuxtjs/auth-next": "^5.0.0-1610115973.9fdaa66",

and here is my nuxt.config.js

export default {
  // Global page headers (https://go.nuxtjs.dev/config-head)
  head: {
    title: 'choosday',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },

  // Global CSS (https://go.nuxtjs.dev/config-css)
  css: [
    '~assets/scss/tailwind.scss',
  ],

  // Plugins to run before rendering page (https://go.nuxtjs.dev/config-plugins)
  plugins: [
  ],

  // Auto import components (https://go.nuxtjs.dev/config-components)
  components: true,

  // Modules for dev and build (recommended) (https://go.nuxtjs.dev/config-modules)
  buildModules: [
    // https://go.nuxtjs.dev/tailwindcss
    '@nuxtjs/vuetify',
    '@nuxtjs/tailwindcss',
  ],
  ssr: true,
  // Modules (https://go.nuxtjs.dev/config-modules)
  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/auth-next'
  ],
  axios: {
    baseURL: 'http://choosapi.test/api'
    // baseURL: 'http://movemeapi.test/api'
  },
  // Build Configuration (https://go.nuxtjs.dev/config-build)
  build: {
    vendor: ['axios', 'vuetify']
  },
  auth: {
    strategies: {
      local: {
        token: {
          property: 'token',
          required: true,
          type: 'bearer'
        },
        user: {
          property: 'user',
          autoFetch: true
        },
        endpoints: {
          login: { url: 'auth/login', method: 'post', propertyName: 'token' },
          logout: { url: 'logout', method: 'get', propertyName: 'token' },
          user: { url: 'auth/profile', method: 'get', propertyName: 'false' },
        }
      }
    }
  },
}
misconfiguration

Most helpful comment

@jbiddulph I think the problem is misconfiguration when migrating from v4 to v5. I updated your config, let me know if it worked :)

  auth: {
    strategies: {
      local: {
        token: {
          property: 'token',
          required: true,
          type: 'Bearer'
        },
        user: {
          property: false, // here should be `false`, as you defined in user endpoint `propertyName`
          autoFetch: true
        },
        // `propertyName` in endpoint was depreacted
        endpoints: {
          login: { url: 'auth/login', method: 'post' },
          logout: { url: 'logout', method: 'get' },
          user: { url: 'auth/profile', method: 'get' },
        }
      }
    }
  },

All 8 comments

Hi @jbiddulph! Can you show me the login response and the user response?

Login

token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9jaG9vc2FwaS50ZXN0XC9hcGlcL2F1dGhcL2xvZ2luIiwiaWF0IjoxNjEwNjQyOTc5LCJleHAiOjE2xxxxthis_is_my_bearer_token_OSwianRpIjoieUtXU0dJcVZrbDY5SzhvaCIsInN1YiI6MTEsInBydiI6IjIzYmQ1Yzg5NDlmNjAwYWRiMzllNzAxYzQwMDg3MmRiN2E1OTc2ZjcifQ.cQVf2Y0nUzPPkTb4mQl8YsoJLManqum-onMDEgOW7I8"
token_type: "bearer"
token_validity: 86400

User:

{"id":11,"name":"John Biddulph","email":"[email protected]","email_verified_at":null,"created_at":"2021-01-13T22:07:16.000000Z","updated_at":"2021-01-13T22:07:16.000000Z"}

It's pretty hard to debug slowly and via an Issue discussion. Can you please do some debugging to figure out _why_ it's not set? Here's a place to start: https://github.com/nuxt-community/auth-module/blob/dev/src/core/auth.ts#L301

I don't have auth.ts file and I'm not using state?!

@jbiddulph I think the problem is misconfiguration when migrating from v4 to v5. I updated your config, let me know if it worked :)

  auth: {
    strategies: {
      local: {
        token: {
          property: 'token',
          required: true,
          type: 'Bearer'
        },
        user: {
          property: false, // here should be `false`, as you defined in user endpoint `propertyName`
          autoFetch: true
        },
        // `propertyName` in endpoint was depreacted
        endpoints: {
          login: { url: 'auth/login', method: 'post' },
          logout: { url: 'logout', method: 'get' },
          user: { url: 'auth/profile', method: 'get' },
        }
      }
    }
  },

That's amazing, thank you so much!

I'm happy to know it worked! :)

I had a more complex issue, and have success fixing that with the next steps:
nuxt.config.js

axios: {
  https: !isLocalMachine // I had a lot of not handled errors inside lib related to https
},
auth: {
  cookie: {
    secure:  !isLocalMachine
  },
  strategies: {
    sessionid: { // strategyName (scheme name)  === cookie name . -> Token._syncToken()
      scheme: "cookie", // should use that key, to call CookieScheme class
      cookie: {
        // my custom backend cookie name, you should migrate to such usage after nuxt-auth@^4 migration
        name: "auth._token.sessionid" 
      }
    }
  }
}
Was this page helpful?
0 / 5 - 0 ratings