首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Nuxt-auth fetchUser CustomStrategy中丢失用户数据

在Nuxt-auth fetchUser CustomStrategy中丢失用户数据
EN

Stack Overflow用户
提问于 2020-11-19 20:05:15
回答 1查看 485关注 0票数 0

大家好!我有自己的自定义策略来获取令牌,一切都很好,但是当刷新页面时,我会丢失用户数据,并且fetchUser无法工作。它不会将参数发送到API以再次获取用户数据。工作流程如下: 1-将参数发送到令牌api并获取令牌2-将参数发送到登录API以获取用户

//nuxt.config.js

代码语言:javascript
复制
customStrategy: {
        _scheme: '~/schemes/customScheme',
        endpoints: {
          login: {
            url: '/api/v1/token',
            method: 'post',
            propertyName: 'token',
            headers: {'x-channel-id': 1}
          },
          user: {
            url: '/api/v1/login',
            method: 'post',
            propertyName: false,
            headers: {'x-channel-id': 1}
          },
          logout: null
        }
      }

customScheme.js

代码语言:javascript
复制
import LocalScheme from '@nuxtjs/auth/lib/schemes/local'

export default class CustomScheme extends LocalScheme {

  _setToken (token) {
    if (this.options.globalToken) {
      // Set Authorization token for all axios requests
      this.$auth.ctx.app.$axios.setHeader(this.options.tokenName, token)
    }
  }
  
  _clearToken () {
    if (this.options.globalToken) {
      // Clear Authorization token for all axios requests
      this.$auth.ctx.app.$axios.setHeader(this.options.tokenName, false)
    }
  }
  
  mounted () {
    if (this.options.tokenRequired) {
      const token = this.$auth.syncToken(this.name)
      this._setToken(token)
    }
    return this.$auth.fetchUserOnce()

  }

  async login (endpoint) {
    if (!this.options.endpoints.login) {
      return
    }

    // Get token
    const result = await this.$auth.request({
      ...endpoint
    },
      this.options.endpoints.login
    )

    // Set token
    if (this.options.tokenRequired) {
      const token = this.options.tokenType
        ? this.options.tokenType + ' ' + result
        : result
        
      this.$auth.setToken(this.name, token)
      this._setToken(token)
    }

    // If result I get and set user
    if (result) {
      const user = await this.$auth.request({
        ...endpoint
      },
        this.options.endpoints.user
      )
      this.$auth.setUser(user);
    }
  }
  
  async fetchUser (endpoint) {
    // User endpoint is disabled.
    if (!this.options.endpoints.user) {
      this.$auth.setUser({})
      return
    }
    
    // Token is required but not available
    if (this.options.tokenRequired && !this.$auth.getToken(this.name)) {
      return
    }
    
    // Try to fetch user and then set
    try{
      const user = await this.$auth.requestWith(
        this.name,
        endpoint,
        this.options.endpoints.login
      )

      this.$auth.setUser(user)
    } catch (error){
      console.log(error)
    }
  }
}

当我在this.$auth.setUser(user) ()方法中设置登录时,一切都很好,应用程序将我重定向到/dashboard页面,用户信息(如角色和电子邮件)显示在navBar上,但是当我刷新页面时,我丢失了用户数据。该应用程序尝试fetchUser,但它给我一个400错误,因为用户和密码没有发送。我不理解的另一件事是为什么async fetchUser (endpoint)中没有定义endpoint参数?我认为这部分是有问题的。

希望你能帮我打招呼

EN

回答 1

Stack Overflow用户

发布于 2020-12-07 20:49:06

我刚刚删除了所有这个库,并执行了自己的自定义Nuxt身份验证https://nemanjadragun92.medium.com/nuxt-js-custom-authentication-245d2816c2f3

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64911313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档