Guest Mode
This feature was added in v0.5.0 of
nuxt-auth
.You can use nuxt-auth
to setup pages that are accessible only when the user is not logged in. This is sometimes called "guest mode". The behavior of such a page is as follows:
- A logged in user visits the page -> redirect to another (likely protected) page,
- A logged out user visits the page -> they are allowed to stay and view it
This behavior is useful for login pages that you don't want to be visitable by logged in users: Why should they go through a login flow again?
Usage
Briefly summarized, you can enable guest mode on a page by passing the following configuration:
Global middleware enabled
definePageMeta({ auth: { unauthenticatedOnly: true, navigateAuthenticatedTo: '/profile', },})
The above will:
- allow only guest (== users that are not logged in) to visit the page
- redirect everyone who is already logged in to
/profile
Note: Setting unauthenticatedOnly: false
above is equivalent to setting auth: false
from the user-perspective, but requires some extra middleware-steps, so is a bit less efficient. Therefore it is recommended to always use auth: false
instead.