简体   繁体   English

Nuxt.js auth 用户已设置但已登录仍为 false

[英]Nuxt.js auth user is set but loggedIn is still false

js app with Nuxt Auth and when I want to log in. User is not set and loggedIn is false带有loggedIn身份验证的 Node.js 应用程序以及当我想登录时。未设置用户且已登录为false

const response = await this.$auth.loginWith('local', { data: {
   email: this.form.email.value,
   password: this.form.password.value,
} });
this.$auth.setUser(response.data.user);
this.$auth.strategy.token.set(response.data.jwt.access_token)
this.$auth.strategy.refreshToken.set(response.data.jwt.refresh_token)

so I wrote this and after that user is set but loggedIn is still false.所以我写了这个,然后设置了用户,但loggedIn仍然是假的。 Here my nuxt.config.js .这是我的nuxt.config.js

auth: {
    strategies: {
      local: {
        scheme: 'refresh',
        token: {
          property: 'access_token',
          maxAge: 1800,
          required: true,
          type: 'Bearer',
        },
        refreshToken: {
          property: 'refresh_token',
          data: 'refresh_token',
          maxAge: 60 * 60 * 24 * 30,
        },
        user: {
          property: 'user',
          autoFetch: true,
        },
        endpoints: {
          login: { url: '/login', method: 'post' },
          refresh: { url: '/refresh', method: 'post', propertyName: false },
          logout: { url: '/logout', method: 'post' },
          user: { url: '/refresh', method: 'post', propertyName: false },
        },
      },
    },
  },

Can you help me with it please?你能帮我吗?

I found solution to my answer.我找到了答案的解决方案。 I wrote my own scheme我写了自己的scheme

import { RefreshScheme } from '~auth/runtime'

export default class CustomScheme extends RefreshScheme {
  // Override `fetchUser` method of `local` scheme
  async fetchUser (endpoint) {

    this.options.endpoints.user = {
      ...this.options.endpoints.user,
      data: {
        token: this.$auth.strategy.refreshToken.get()
      }
    }

    // Token is required but not available
    if (!this.check().valid) {
      return
    }

    // User endpoint is disabled.
    if (!this.options.endpoints.user) {
      this.$auth.setUser({})
      return
    }

    // Try to fetch user and then set
    return this.$auth.requestWith(
      this.name,
      endpoint,
      this.options.endpoints.user
    ).then((response) => {
      const user = response.data.user

      // Transform the user object
      const customUser = {
        ...user,
        fullName: user.name,
        roles: ['user']
      }

      // Set the custom user
      // The `customUser` object will be accessible through `this.$auth.user`
      // Like `this.$auth.user.fullName` or `this.$auth.user.roles`
      this.$auth.setUser(customUser)

      return response
    }).catch((error) => {
      this.$auth.callOnError(error, { method: 'fetchUser' })
    })
  }
}

Apparently was the problem by getting user from propertyName .显然是从propertyName获取用户的问题。 I replace code with my own response const user = response.data.user and now it seems working:)我用我自己的响应const user = response.data.user替换代码,现在它似乎工作了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM