nuxt-community / auth-module

Zero-boilerplate authentication support for Nuxt 2
https://auth.nuxtjs.org
MIT License
1.93k stars 925 forks source link

Fetching User after login even though autoFetchUser is set false #776

Closed gitsakil09 closed 3 years ago

gitsakil09 commented 4 years ago

I have followed the instructions but still It is trying to fetch the user after login request. For example when i try to login my system returns me a message, what I want is to display that message but auth is automatically calling the user routes and when the credentials are wrong then it is setting the undefined as token value. My Nuxt configs are here..

strategies: {
      local: {
        endpoints: {
          // login url
          login: {
            url: 'login',
            method: 'post',
            propertyName: 'data.token'
          },
          // user: false,
          user: { url: '/user/info', method: 'get', propertyName: '' },
          logout: false
        }
      }
    },
    rewriteRedirects: true,
    autoFetchUser: false,
  }

BTW I am getting the user info nicely but what I want is to show the message when credentials are wrong and disabling the auto fetch user My Login.vue

    await _this.$auth
          .loginWith('local', {
            data: _this.loginInfo
          })
          .then((response) => {
            _this.$auth.setUser(response.data.user) // this data is ok
          })
          .catch((error) => {
            // here I am getting JWT token error and this error comes from /user/info route
          })

Thanks :)

talal424 commented 4 years ago

autoFetchUser should be added to local not strategies

this would disable fetching user after a successful login. if you want disable it entirely set user to false

change propertyName's value to token there is no need for data.token unless the response's data is like {user:{}, data:{token:''}}

to show any errors in user input from backend like incorrect credentials

await _this.$auth
.loginWith('local', {
  data: _this.loginInfo
})
.then((response) => {
  // check the response.data for any messages before setting the user for example:
  if (response.data.messages) {
    // show messages
    return
  }
  _this.$auth.setUser(response.data.user) // this data is ok
})
.catch((error) => {
  // here I am getting JWT token error and this error comes from /user/info route
})
mjmnagy commented 4 years ago

Im having the same/similar issue

//nuxt.config.js
/*
   ** Auth module configuration
   ** See https://auth.nuxtjs.org/#
   */
  auth: {
    strategies: {
      local: {
        endpoints: {
          login: { url: 'auth/login', method: 'post' },
          user: false,
          logout: { url: 'auth/logout', method: 'get' }
        },

        tokenRequired: true,
        autoFetchUser: false
        //   tokenType: false
      }
    }

image

If i try and use loginWith('local'i getStrategy local is not defined!`

Im ultimately going to use a cookie auth system

JoaoPedroAS51 commented 3 years ago

Closing here, as this is an old issue and has already been answered by @talal424 :)

m-shum commented 11 months ago

Still having this issue in 2023. local: {autoFetchUser: false} doesn't prevent user autofetch. Neither does local: {user: false} or local: {user: {property: false, autoprefetch: false}}. endpoints.user is set to false.