auth-module icon indicating copy to clipboard operation
auth-module copied to clipboard

$auth.loggedIn is always flase

Open siiiido opened this issue 2 years ago • 10 comments

Version

module: 5.0.0-1648802546.c9880dc
nuxt: 2.15.8

Nuxt configuration

mode:

  • [ ✓] universal
  • [ ] spa

Nuxt configuration

    auth: {
    strategies: {
      local: {
        token: {
          property: 'token',
          global: true,
        },
        user: {
          property: 'user', 
          autoFetch: true,
        },
        endpoints: {
          login: {
            url: 'http://127.0.0.1:8000/login/',
            method: 'post',
          },
          logout: {
            url: 'http://127.0.0.1:8000/logout/',
            method: 'get',
          },
          user: {
            url: 'http://127.0.0.1:8000/user/',
            method: 'get',
          },
        },
      },
    },
  },

What is expected?

When the user logins in with $auth.loginWith, $auth.loggedIn must return true and $auth.user also must return Object.

What is actually happening?

When the user logins in with $auth.loginWith, $auth.loggedIn and $auth.user are always false. If I use $auth.setUser when the user logins in, $auth.user return Object but $auth.loggedIn return always false.

Steps to reproduce

Additional information

Checklist

  • [ ✓] I have tested with the latest Nuxt version and the issue still occurs
  • [ ✓] I have tested with the latest module version and the issue still occurs
  • [ ✓] I have searched the issue tracker and this issue hasn't been reported yet

siiiido avatar Apr 18 '22 15:04 siiiido

I also have the same issue after calling the loginWith method the token didn't set. @siiiido did you already figured out the solution for this one ?

dechalico avatar Apr 21 '22 06:04 dechalico

I also have the same issue after calling the loginWith method the token didn't set. @siiiido did you already figured out the solution for this one ?

No. I tried several times but it still returns false. I just builed JWT Authentication using Vuex. If you want to bulid like me, I referenced that.

Vue2

Vue3

siiiido avatar Apr 22 '22 15:04 siiiido

Hello, I just installed fresh week ago and same thing, after loginWith goes on and even set token to cookies, loggedIn is false. and because its False user Endpoint is not even called.

By chance any older version which is working ? (have "@nuxtjs/auth-next": "5.0.0-1648802546.c9880dc",)

Songoo7 avatar May 31 '22 16:05 Songoo7

I had "refresh" scheme. In that scheme if "refreshToken" property is defined, seems nuxt-auth require it to be pressend also in initial login endpoint. Which should return token and also refresh_token values. If login endpoint return only token, no error mesage or no notice is shown, after login token is saved to cookies but everything ends here and no user request is called at all.

So adding refresh_token to api login response helped me, after login user endpoint is called. This may not be fully related o this issue problem, just noting.

Songoo7 avatar Jun 01 '22 08:06 Songoo7

@Songoo7 I solve it by doing this one.

local { token: { property: 'data.token', required: true, global: true, name: 'Authorization', type: 'Bearer', }, user: { property: 'data.member', autoFetch: false, }, endpoints: { login: { url: '/some-login-url', method: 'post' }, logout: { url: '/some-logout-url', method: 'post' }, user: { url: '/some-member-details-url', method: 'get' }, }, }

in the local option if you have "user" field and not set to false like user: false, need to provide user endpoint in the endpoint property like endpoints: {user: "/some-url-here"} this will trigger after loginWith call, if the user is success fetch then it will set the $auth.loggedIn to true else false.

if you don't have user endpoint just set if to false, after loginWith call it will set the $auth.loggedIn = true, ex:

local: { user: false, endpoints: { user: false } }

dechalico avatar Jun 05 '22 04:06 dechalico

it not work for me:

strategies: { local: false, graphql: { // custom scheme: '~/api/graphqlLogin', user: false, endpoints: { user: false } } },

and not work with: ( https://vorlume.com/nzesalem/stories/nuxt-graphql-auth-scheme )

strategies: { local: false, graphql: { // custom scheme: '~/api/graphqlLogin', } },

giovannimanzoni avatar Jun 06 '22 12:06 giovannimanzoni

I finaly solve it with this workaround: ( in comment on https://stackoverflow.com/questions/71816374/nuxt-auth-v5-not-setting-loggedin-when-use-login-logout )

const ret = await this.$auth.loginWith('graphql', credentials) if (ret) { await this.$auth.setUser(ret) this.$auth.$storage.setUniversal('loggedIn', true) // @fixme -- workaround }

work with last version, @nuxtjs/[email protected]

giovannimanzoni avatar Jun 06 '22 12:06 giovannimanzoni

For me, setting user.property to false does the trick

auth: {
    strategies: {
      customStrategy: {
          user: {
              property: false,
          },
          ...
    },
}

manuelodelain avatar Jul 06 '22 08:07 manuelodelain

hi @siiiido,

what is your login and user endpoints looks like ?

are they like this ?

// login endpoints 

{
   "auth_token": "your_token"
}

// user endpoints
{
   "id": user_id,
   "email": user_email
}

if it is then you have to make sure your configuration is correct, for example above: the config would be

auth: {
    strategies: {
      local: {
        token: {
          property: 'auth_token',
          global: true,
        },
        user: {
          property: false 
          autoFetch: true,
        },
        endpoints: {
          // your api end point
        },
      },
    },
  },

hope this will help

antandev avatar Aug 03 '22 06:08 antandev

Leaving this here to hopefully help someone. I have a Nuxt SSR setup and for the longest time was struggling with this.$auth working after logging in. The kicker is that $auth only appears to work after a refresh - (after the server has the data). If you want to use the auth flags / data on the client (before a refresh) - you should use:

this.$store.state.auth.loggedIn 

As mentioned in the docs here: https://auth.nuxtjs.org/api/auth#loggedin

thatericsmith avatar Nov 28 '22 17:11 thatericsmith