【问题标题】:Nuxtjs Auth module with Laravel Sanctum ProviderNuxtjs Auth 模块与 Laravel Sanctum Provider
【发布时间】:2022-01-25 05:33:42
【问题描述】:

我已将 nuxt/auth 模块与 Laravel Sanctum 提供程序集成,并且登录工作正常,问题是何时我想记住用户。

我向 api 发送了一个名为 remember 的布尔参数,在 api 中,选项 remember 分配得很好,令牌保存在用户表中,并且 nuxt/auth 模块设置了 cookie remember_web_59ba36addc2b2b2b2f9401580f014c7f58ea4e30989d 但如果我设置了 SESSION_LIFETIME到 5 分钟,当我在 5 分钟后刷新页面时,用户断开连接并且直到 2027 年才保持会话​​,这是分配给 cookie 的日期,我附上了一张带有 cookie 日期的图像。

nuxt.config.js

    auth: {
    strategies: {
      'laravelSanctum': {
        provider: 'laravel/sanctum',
        url: process.env.BASE_URL
      },
    }
  },

  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    baseUrl: process.env.BASE_URL,
    credentials: true
  },

在登录页面上

this.$auth.loginWith('laravelSanctum', {
   data: this.form
})

与时俱进的饼干

【问题讨论】:

    标签: laravel nuxt.js laravel-sanctum nuxt-auth


    【解决方案1】:

    在 nuxt.config.js 中

    axios: {
            baseURL: 'http://localhost/twillo-api/api'
          },
    auth: {
        redirect: {
          login: '/account/login',
          callback: '/account/login',
          home: '/'
        },
        strategies: {
          local: {
            token: {
              property: 'data.accessToken',
              required: true,
              global: true,
              maxAge: 43200,
              type: 'Bearer',
              name: 'Authorization'
            },
            user: {
              property: 'user',
              autoFetch: false
            },
            endpoints: {
              login: { url: '/login', method: 'post' },
            },
            localStorage: {
              prefix: 'auth.'
            }
          }
        },
      },
    

    在登录页面上
    setUniversal 会将用户记录存储在 cookie 中。

      this.$auth.loginWith('local', { data: payload })
                        .then(response => {
                          
                            if(response.data.status) {
                                this.$auth.setUser(response.data.data.userData)
                                this.$auth.$storage.setUniversal('user', response.data.data.userData)
                                this.$auth.$storage.setUniversal('loggedIn', true)
                              
                            }else {
                              this.invalidCredential= response.data.message
                            }
                        }).catch(error => {
                            this.backendErrors = error.response.data.errors
                        })
    

    【讨论】:

    • 我正在使用 Laravel Sanctum 提供程序,它使用 cookie 架构而不是带有令牌的本地架构,登录工作正常,但记住登录不起作用
    • 其实用local可以在nuxt auth(this.$auth.setUser(response.data.data.userData))中保存登录用户,然后通过this.$auth获取用户记录。用户名。 setUniversal 函数会将用户记录存储在 cookie 中,然后您可以通过 nuxt auth 获取。
    猜你喜欢
    • 2021-04-27
    • 2020-08-16
    • 2020-09-18
    • 2020-07-25
    • 2020-07-26
    • 2021-07-01
    • 1970-01-01
    • 2022-10-21
    • 2022-12-15
    相关资源
    最近更新 更多