Mustafa Alammar
Mustafa Alammar

Reputation: 656

Nuxt Auth Module with Auth0 Provider Causing Redirect Loop

I have a SPA built using Nuxt and @nuxtjs/auth that is configured to connect to Auth0 via the built-in Auth0 provider in nuxt-auth. The resulting app viewed through webpack's server via yarn dev has a redirect loop after login. Login is successful but the page is then redirected back to the /callback url to go through the OAuth flow again.

Here is my 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
  }
}

Here is my directory structure:

├── 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

Upvotes: 3

Views: 2189

Answers (1)

Mustafa Alammar
Mustafa Alammar

Reputation: 656

Figured it out. The issue was caused by putting the initialization call for nuxt inside of the mounted() method of my top navigation.

I changed it to:

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

Upvotes: 3

Related Questions