Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update abort-navigation.md
  • Loading branch information
DamianGlowala authored Aug 25, 2022
commit 6f8b2be1a6815b5eda2308871131b0a649cd183c
56 changes: 29 additions & 27 deletions docs/content/3.api/3.utils/abort-navigation.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
# `abortNavigation`

`abortNavigation` is a helper function that prevents the navigation from taking place.
`abortNavigation` is a helper function that prevents navigation from taking place and throws an error if one is set as a parameter.

## Usage
::alert{type="warning"}
`abortNavigation` is only usable inside a [route middleware handler](/guide/directory-structure/middleware).
::

## Type

```ts
abortNavigation(err?: Error | string): false
```

`abortNavigation` takes one optional argument which can be of type `string` or an `Error` object.
## Parameters

- **err**: Optional error to be thrown byΒ `abortNavigation()`.
### `err`

::alert{type="warning"}
`abortNavigation()` is only usable inside a [route middleware handler](/guide/directory-structure/middleware).
::
- **Type**: [`Error`](https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/Error) | `string`

Inside a route middleware handler, `abortNavigation()` will abort navigation, and throw an error if one is set as a parameter.
Optional error to be thrown byΒ `abortNavigation`.

## Examples

The example below shows how you can use `abortNavigation` in route middleware to prevent unauthorised route access.
The example below shows how you can use `abortNavigation` in a route middleware to prevent unauthorized route access:

```jsx
// middleware/auth.ts
```ts [middleware/auth.ts]
export default defineNuxtRouteMiddleware((to, from) => {
const user = useState('user')
if (!user.value.isAuthorized) {
const user = useState('user')

if (!user.value.isAuthorized) {
abortNavigation()
Comment thread
DamianGlowala marked this conversation as resolved.
Outdated
Comment thread
DamianGlowala marked this conversation as resolved.
Outdated
}
return navigateTo('/edit-post')

return navigateTo('/edit-post')
})
```

### `err` as a string
### `err` as a String

You can pass the error as a `string`.
You can pass the error as a string:

```ts [middleware/auth.ts]
export default defineNuxtRouteMiddleware((to, from) => {
const auth = useState('auth')
if (!user.value.isAuthorized) {
abortNavigation('This feature requires special permission.')
const auth = useState('auth')

if (!user.value.isAuthorized) {
abortNavigation('Insufficient permissions.')
}
})
```

### `err` as an Error object
### `err` as an Error Object

You can pass the error as an `Error` object that includes the error code and a message.
You can pass the error as an [`Error`](https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/Error) object, e.g. caught by the `catch`-block:

```ts [middleware/auth.ts]
export default defineNuxtRouteMiddleware((to, from) => {
const auth = useState('auth')
if (to.params.id === '1') {
abortNavigation({
statusCode: 401,
statusMessage: 'This feature requires special permission.'
})
try {
/* code that might throw an error */
} catch (err) {
abortNavigation(err)
}
})
```
Expand Down