---
url: "https://vueda.dev/v3/reference/api/js/router/guards/functions/requireAuth.md"
title: "requireAuth"
---

# requireAuth

## Overview

Require the user to be authenticated.

## Signature

`requireAuth(redirectTo, to, router, pinia)`

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| <span id="param-redirectTo">redirectTo</span> | `string \| RouteLocationAsRelativeGeneric \| RouteLocationAsPathGeneric` | yes | Where to redirect if not authenticated. |
| <span id="param-to">to</span> | `RouteLocationNormalizedGeneric` | yes | The target route. |
| <span id="param-router">router</span> | `Router` | yes | The router instance. |
| <span id="param-pinia">pinia</span> | `Pinia` | yes | The Pinia instance. |

### Returns

`Promise`

The redirect route if needed.
 Rejects with the request error when the current-user request fails, which rejects the navigation.

## Examples

```js
import { requireAuth } from "@vueda/router/guards";
import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/auth-required/',
      name: 'auth-required',
      component: () => import('@/views/ViewAuthRequired.vue'),
      beforeEnter: (to, from) => requireAuth({ name: "log-in" }, to, router),
    },
    // other routes
  ],
});

export default router;
```

## Source

`client/lib/router/guards.js:125`
