> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/nuxt/nuxt/llms.txt
> Use this file to discover all available pages before exploring further.

# abortNavigation

> Prevent navigation from taking place and throw an error if one is set as a parameter.

<Warning>
  `abortNavigation` is only usable inside a route middleware handler.
</Warning>

## Type Signature

```ts theme={null}
function abortNavigation(err?: Error | string): false
```

## Parameters

<ParamField path="err" type="Error | string" optional>
  Optional error to be thrown by `abortNavigation`. Can be an Error object or a string message.
</ParamField>

## Return Value

Returns `false` to abort the navigation.

## Examples

### Basic Usage

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

```ts theme={null}
// app/middleware/auth.ts
export default defineNuxtRouteMiddleware((to, from) => {
  const user = useState('user')

  if (!user.value.isAuthorized) {
    return abortNavigation()
  }

  if (to.path !== '/edit-post') {
    return navigateTo('/edit-post')
  }
})
```

### Using String Error Message

You can pass the error as a string:

```ts theme={null}
// app/middleware/auth.ts
export default defineNuxtRouteMiddleware((to, from) => {
  const user = useState('user')

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

### Using Error Object

You can pass the error as an Error object, e.g. caught by the `catch`-block:

```ts theme={null}
// app/middleware/auth.ts
export default defineNuxtRouteMiddleware((to, from) => {
  try {
    /* code that might throw an error */
  } catch (err) {
    return abortNavigation(err)
  }
})
```

## See Also

* [Route Middleware Guide](/guide/directory-structure/middleware)
* [`navigateTo`](/api/utils/navigate-to)
