Skip to content

Commit

Permalink
🔄 synced local 'content/2.nuxt-auth/' with remote 'docs/content/'
Browse files Browse the repository at this point in the history
  • Loading branch information
sideborg committed Sep 23, 2023
1 parent a82cf37 commit bbf6af2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion content/2.nuxt-auth/3.application-side/4.protecting-pages.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Protecting Pages

`nuxt-auth` offers different approaches to protect pages:

1. Global protection: Protects all pages with manual exceptions
2. Local protection: Protects specific pages
3. Custom middleware: Create your own middleware
Expand Down Expand Up @@ -48,6 +49,7 @@ That's it! Every page of your application will now need authentication for the u
### Disabling the global middleware locally

To disable the global middleware on a specific page only, you can use the [`definePageMeta` macro](https://nuxt.com/docs/api/utils/define-page-meta#definepagemeta) to turn `auth` off:

```vue
<!-- file: ~/pages/index.vue -->
<template>
Expand All @@ -61,7 +63,6 @@ definePageMeta({ auth: false })

Note: This only works on `pages/`. It notably does not work inside the `app.vue`.


## Local middleware

To protect specific pages with a middleware, you can use the [`definePageMeta` macro](https://nuxt.com/docs/api/utils/define-page-meta#definepagemeta) to turn `auth` on:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Session Access and Management

## `useAuth` Composable

The `useAuth` composable is your main gateway to accessing and manipulating session-state and data. Here's the main methods you can use:
::code-group
```ts [authjs]
Expand Down Expand Up @@ -132,6 +134,18 @@ This is a configuration option available to dynamically type the `SessionData` t

`nuxt-auth` uses [unjs/knitwork](https://github.com/unjs/knitwork) to generate the correct typescript interface from the type you provide.

## Force refetching the session (`local` provider only)

Calling `getSession` will by default **only** refetch the current session if the token returned by `useAuthState` is defined.
Passing the `{ force: true }` option will always update the current session:

::code-group
```ts [local]
// force update the current session
await getSession({ force: true })
```
::

## Redirects

You can also pass the `callbackUrl` option to both the `signIn`, the `signOut` and the `getSession` methods. This allows you to redirect a user to a certain pages, after they've completed the action. This can be useful when a user attempts to open a page (`/protected`) but has to go through external authentication (e.g., via their google account) first.
Expand Down
14 changes: 14 additions & 0 deletions content/2.nuxt-auth/v0.6/3.application-side/4.protecting-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ export default defineNuxtConfig({

That's it! Every page of your application will now need authentication for the user to visit it.

### Middleware Options

#### `unauthenticatedOnly`

Whether to only allow unauthenticated users to access this page. Authenticated users will be redirected to `/` or the route defined in `navigateAuthenticatedTo`

#### `navigateAuthenticatedTo`

Where to redirect authenticated users if `unauthenticatedOnly` is set to true

#### `navigateUnauthenticatedTo`

Where to redirect unauthenticated users if this page is protected

### Disabling the global middleware locally

To disable the global middleware on a specific page only, you can use the [`definePageMeta` macro](https://nuxt.com/docs/api/utils/define-page-meta#definepagemeta) to turn `auth` off:
Expand Down

0 comments on commit bbf6af2

Please sign in to comment.