【问题标题】:Nuxt Auth Module with Auth0 Provider Causing Redirect LoopNuxt Auth 模块与 Auth0 提供程序导致重定向循环
【发布时间】:2019-12-03 03:58:30
【问题描述】:

我有一个使用 Nuxt 和 @nuxtjs/auth 构建的 SPA,它被配置为通过 nuxt-auth 中的内置 Auth0 提供程序连接到 Auth0。通过yarn dev 通过 webpack 的服务器查看的生成的应用程序在登录后有一个重定向循环。登录成功,但页面随后被重定向回/callback url 以再次通过 OAuth 流程。

这是我的 nuxt.config.js:

import colors from 'vuetify/es5/util/colors'
import dotenv from 'dotenv'

// Get env vars
dotenv.config()

export default {
  mode: 'spa',
  /*
   ** Plugins to load before mounting the App
   */
  plugins: ['~/plugins/axios', '~/plugins/shortkey'],
  /*
   ** Nuxt.js modules
   */
  modules: [
    '@nuxtjs/dotenv',
    '@nuxtjs/vuetify',
    '@nuxtjs/axios',
    '@nuxtjs/auth',
    '@nuxtjs/eslint-module'
  ],
  /*
   ** Axios module configuration
   ** See https://axios.nuxtjs.org/options
   */
  axios: {},

  auth: {
    redirect: {
      callback: '/callback'
    },
    strategies: {
      auth0: {
        domain: `mydomain-${process.env.TENANCY}.auth0.com`,
        client_id: 'myClientId',
        audience: 'https://my-api.mydomain.com',
        scope: [
          'openid',
          'profile',
          'email',
          'userinfo',
          'user:*',
          'user:read:all'
        ]
      }
    },
    plugins: ['~/plugins/cdnAuth.js']
  },

  router: {
    middleware: ['auth']
  },

  /*
   ** vuetify module configuration
   ** https://github.com/nuxt-community/vuetify-module
   */
  vuetify: {
    theme: {
      primary: colors.blue.darken2,
      accent: colors.grey.darken3,
      secondary: colors.amber.darken3,
      info: colors.teal.lighten1,
      warning: colors.amber.base,
      error: colors.deepOrange.accent4,
      success: colors.green.accent3
    }
  },
  /*
   ** Build configuration
   */
  build: {
    /*
     ** You can extend webpack config here
     */
    extend(config, ctx) {}
  },

  server: {
    port: 3005
  },

  env: {
    API_KEY: process.env.API_KEY,
    CDN_URL: process.env.CDN_URL
  }
}

这是我的目录结构:

├── README.md
├── assets
│   ├── favicon.png
│   ├── logo_black.svg
│   └── style
├── components
│   ├── index.js
├── jest.config.js
├── layouts
│   ├── README.md
│   ├── default.vue
│   ├── dialog.vue
│   └── error.vue
├── middleware
│   └── README.md
├── mixins
│   ├── README.md
│   └── index.js
├── nuxt.config.js
├── package.json
├── pages
│   ├── README.md
│   ├── callback.vue
│   ├── index.vue
│   ├── login.vue
├── plugins
│   ├── README.md
│   ├── axios.js

【问题讨论】:

    标签: javascript authentication auth0 nuxt.js


    【解决方案1】:

    想通了。该问题是由于将 nuxt 的初始化调用放在我的顶部导航的 mount() 方法中引起的。

    我改成:

    mounted() {
      !this.$auth.loggedIn && this.$auth.loginWith('auth0')
    },
    

    【讨论】:

    • 这个问题在 Firefox 中经常发生,但在 Chrome 或 Edge 中不会发生。添加登录检查并不能可靠地帮助。我需要在调用 1 秒的 loginWith 方法之前添加一个超时来解决问题。打算尝试更小的时间窗口让 auth 模块意识到用户已通过身份验证,但我真的不想发布这样的代码哈哈
    猜你喜欢
    • 2021-01-19
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    相关资源
    最近更新 更多