> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

# Method name: beforeRouter(request: WixRouterRequest)

# Method package: wixRouter

# Method menu location: wixRouter --> beforeRouter

# Method Link: https://dev.wix.com/docs/velo/events-service-plugins/routers/service-plugins/wix-router/before-router.md

# Method Description:

Registers a hook that is called before a router.

The `beforeRouter` hook is a data binding router hook that is triggered
before the router with the specified [prefix](#prefixes) has bound the data to the page.
The router can be a code router or the data binding router that binds data
to a dynamic page.

The `beforeRouter()` function is not a function that you call from your code.
You define the function in a file named **routers.js** in the Code File's Backend section of the [Velo Sidebar](https://dev.wix.com/docs/develop-websites/articles/workspace-tools/velo-workspace/workspaces/wix-editor-working-with-the-code-sidebar.md).
The function is called as described above.

Use this hook with a code router to route requests to a different page or return an error response.
For example, you can check who is requesting the page and then decide based on
the user's role whether to let the router continue to the next step or to return
an error type response code.

The function receives a [`WixRouterRequest`](https://dev.wix.com/docs/velo/events-service-plugins/routers/service-plugins/wix-router/wix-router-request/introduction.md)
object containing information about the incoming request.

The function returns a [`WixRouterResponse`](https://dev.wix.com/docs/velo/events-service-plugins/routers/service-plugins/wix-router/wix-router-response/data.md)
object that causes the router to continue its routing, or respond with an
HTTP response code.

Typically, the response is created using one of the [`next()`](#next),
[`forbidden()`](#forbidden), [`notFound()`](#notFound), [`redirect()`](#redirect),
or [`sendStatus()`](#sendStatus) functions.



# Method Code Examples:

*** Note: do not assume any prop names or enum values other than the ones in the example.

## Before router stub

```javascript
// In routers.js

export function myRouter_beforeRouter(request) {
  // before router code ...
}

```

## Basic before router hook

```javascript
// In routers.js

import {forbidden, next} from 'wix-router';

export function myRouter_beforeRouter(request) {
  if (some_condition)
    return next();
  else
    return forbidden();
}

```

## Before router hook that restricts access based on user role

```javascript
// In routers.js

import {forbidden, next} from 'wix-router';

export function myRouter_beforeRouter(request) {
  if (request.path.length > 1 && request.path[0] === "admin") {
    if (request.user && request.user.role == "Admin")
      return next();
    else
      return forbidden();
  }

  return next();
}

```

---