Nuxt auth redirect when not logged in not working

Viewed 701

I'm using Nuxt and the Auth module. I'm trying to redirect users to a login page when they aren't logged in.

nuxt.config.js:

auth: {
  redirect: {
    login: '/account/inloggen',
    logout: '/account/uitloggen',
    callback: '/account/aanmelden',
    home: '/'
  },
  strategies: {
    google: {
      access_token_endpoint: '****/social?identityProvider=Google',
      access_type: 'offline',
      client_id: '*****.apps.googleusercontent.com',
      grant_type: 'authorization_code',
      redirect_uri: 'http://localhost:3000/account/aanmelden',
      response_type: 'code',
      token_key: 'jwt',
      userinfo_endpoint: '****/user/profile'
    }
  },
  cookie: {
    options: {
      maxAge: 86400
    }
  }
  // localStorage: false
},

I'm reading the docs

login: User will be redirected to this path if login is required.

But I can't find where/how I require a user to log in.

The home: '/' redirect does work, when the user logs in they get redirected to the root of the app.

2 Answers

Found my answer on Github

I've ran into this issue as well. My mistake was having auth: false set on my login page. If you have it, just remove it.

<script>
export default {
  auth: false
}
</script>

You have to enable the auth middleware either globally

nuxt.config.js

router: {
  middleware: ['auth']
}

or per route

Something.vue

<script>
export default {
  middleware: 'auth'
}
</script>
Related