> For the complete documentation index, see [llms.txt](https://docs.audit.venturytechnology.fr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.audit.venturytechnology.fr/authentifications/middleware-proxy.ts.md).

# Middleware (proxy.ts)

Le middleware intercepte **toutes les requêtes** et applique ces règles :&#x20;

{% code overflow="wrap" %}

```
/auth -> bloquer si la session existe, redirection vers /result 
/result -> bloquer si la session n'existe pas, redirection vers /auth
/auth/mfa -> bloquer si il le code n'existe pas, redirection vers /auth 
/auth/verify -> bloquer si l'utilisateur n'est pas vérifier, redirection vers/auth/mfa
```

{% endcode %}

{% code overflow="wrap" %}

```typescript
import { NextRequest, NextResponse } from "next/server";

export function proxy(request: NextRequest) {
  const path = request.nextUrl.pathname;
  const session = request.cookies.get("auth_session")?.value;
  const mfaExpected = request.cookies.get("mfa_expected_code")?.value;
  const mfaVerified = request.cookies.get("mfa_verified")?.value;

  if (session && path.startsWith("/auth")) {
    return NextResponse.redirect(new URL("/result", request.url));
  }

  if (path.startsWith("/result") && !session) {
    return NextResponse.redirect(new URL("/auth", request.url));
  }

  if (path.startsWith("/auth/mfa") && !mfaExpected) {
    return NextResponse.redirect(new URL("/auth", request.url));
  }

  if (path.startsWith("/auth/verify") && !mfaVerified) {
    return NextResponse.redirect(new URL("/auth/mfa", request.url));
  }

  return NextResponse.next();
}

export const config = {
  matcher: [
    "/((?!api|_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
  ],
};

```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.audit.venturytechnology.fr/authentifications/middleware-proxy.ts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
