nuxt-community / auth-module

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

Login not working while data successfully returning from backend #953

Closed shakhawatfci closed 3 years ago

shakhawatfci commented 3 years ago

I have laravel passport based api where i am requesting from my nuxt app with login data and data responding in below format

{ "status": "success", "token": "MySecret-Token", "user": { "id": 1002, "name": "Shakhwat hossain", "avatar": "https:someimageapi.com/avatar.jpg", "mobile_no": "01834394****", "email": "shakhawat@limmexbd.com", "area_id": 1, "sub_area_id": 0, }

but login not working in nuxt application using nuxt-auth module version 5.0.0-1608140074.47635f0

here is my login method

async login() {
  try {
    let response = await this.$auth.loginWith("local", { data: this.form });
  } catch (err) {
    console.log(err.response.data.message);
  }
},

and here is my nuxt auth setting in nuxt.config.js

 auth: {
    strategies: {
      local: {
        token: {
          property: 'token',
          required: false,
          type: 'Bearer',
          // maxAge: 31536000,
        },
        user: {
          property: 'user',
          autoFetch: false,
          propertyName: false
        },
        endpoints: {
          login: { url: '/login', method: 'post' },
          logout: { url: '/logout', method: 'post' },
          user: { url: '/user', method: 'get' }
        }
      }
    }
  },
bmulholland commented 3 years ago

Could you post a repro, or at least more details on what "not working" means?

shakhawatfci commented 3 years ago

Could you post a repro, or at least more details on what "not working" means?

token and user information which is coming from backend not getting stored at vuex , localstorage . or not even in cookies if i set it manually it's getting disappear after page change

bmulholland commented 3 years ago

I don't have your code running and so can only be of limited help - you'll need to be even more specific. Is the information getting returned by the backend? Is it ever getting set by auth? When, exactly, does it go away? Have you searched for other issues in this repo that might match what you're encountering?

shakhawatfci commented 3 years ago

Data returning from backend in this format

{
"status": "success",
"token": "MySecret-Token",
"user": {
    "id": 1002,
    "name": "Shakhwat hossain",
    "avatar": "https:someimageapi.com/avatar.jpg",
    "mobile_no": "01834394****",
    "email": "shakhawat@limmexbd.com",
    "area_id": 1,
    "sub_area_id": 0,
}

after loginwith method resolved i am trying to redirect user to /dashboard but it redirecting back to login page because login no getting set .

here is response after hit to api server_return

here is the condition of cookie storage

and vuex state vuex

shakhawatfci commented 3 years ago

i don't know the newest version of auth-module compatible with this version of nuxt

  "dependencies": {
    "@bachdgvn/vue-otp-input": "^1.0.8",
    "@nuxtjs/auth-next": "^5.0.0-1608461079.ae1e5db",
    "@nuxtjs/axios": "^5.12.4",
    "bootstrap": "^4.5.2",
    "bootstrap-vue": "^2.17.3",
    "cookieparser": "^0.1.0",
    "core-js": "^3.6.5",
    "js-cookie": "^2.2.1",
    "nuxt": "^2.14.6"
  },
adesousa commented 3 years ago

Hi @shakhawatfci, same problem here,with same auth-next and nuxt version. After reading your thread, I resolved the probleme with this configuration in nuxt.config.js:

local: {
        token: {
          property: 'access_token', // as my backend send access_token instead of token
          required: true, 
          type: 'Bearer',
        },
        endpoints: {
          login: { url: 'login', method: 'post' },
          user: { url: 'me', method: 'get', property: 'user' },
          logout: false,
        },

Hope this helps...

shakhawatfci commented 3 years ago

I solved the problem by set manually user and token as below in login method

   async login() {
      try {
        let response = await this.$auth.loginWith("local", { data: this.form });

        this.$auth.setUserToken(response.data.token);
        this.$auth.setUser(response.data.user);
      } catch (err) {
        console.log(err.response.data.message);
      }
    },
JoaoPedroAS51 commented 3 years ago

Hi @shakhawatfci ! I'm happy to know that it's working now! :)

I just would suggest to set token.required to true in your config, so you don't need to manually set the token after login.

shakhawatfci commented 3 years ago

Hi @shakhawatfci ! I'm happy to know that it's working now! :)

I just would suggest to set token.required to true in your config, so you don't need to manually set the token after login.

Thanks it works .