Aontaigh
Aontaigh

Reputation: 198

NuxtJS and Auth Route Exemptions

I'm current using Nuxt.js and the authentication module it comes with.

Is it possible to create exemptions in auth to prevent automatic redirection to the login page when a user is not logged in?

Take the following as an example:

  1. User is not logged in and navigates to: http://localhost:3000/#/password-reset

  2. They will be redirected to the login screen: http://localhost:3000/#/login

I want to create an exemption so that all routes are protected by auth, which they already are, with the exception of the page to reset passwords (http://localhost:3000/#/password-reset).

How is this possible?

Upvotes: 0

Views: 511

Answers (1)

ManUtopiK
ManUtopiK

Reputation: 4724

I assume your are using nuxt-auth.

Create a middleware middleware/isAuth.js:

export default function({ app, error }) {
  if (!app.$auth.loggedIn) {
    // If you want to redirect to login page
    app.router.push('/password-reset')

    // If you want to throw error, use this:
    return error({
      statusCode: 403,
      message: 'You are not allowed to see this'
    })
  }
}

In page you want to protect, declare your middleware:

export default {
    ...
    middleware: ['isAuth'],
    ...
}

It will prevent page if user is not logged in. To log user, you should use loginWith.

Upvotes: 1

Related Questions