# Middleware

It is possible to create a custom function that is executed before or after the function associated with an endpopint, the function will be created with the desired name and later it will be passed as a variable.

```julia
function authenticate(request, HTTP)
  isAuthenticated = false

  if (request.params["status"] === "authenticated")
    isAuthenticated = true
  end

  return request, HTTP, isAuthenticated
end
```

The execution of the middleware can only be achieved with the funcionm format, it cannot with the `@route` and `@page` macros. The following structure must be specified where myfunction is the function associated with the path `"/ verify /: status"`. In line number 13, the order in which the functions will be executed is indicated, first the **`middleware`** function and then the **`myfunction`** function.

```julia
Get("/verify/:status",

  (result(;middleware=authenticate) = (request, HTTP)-> begin

  myfunction = (request, HTTP, isAuthenticated)-> begin

      if (isAuthenticated == false )
          return  HTTP.Response(403,string("Unauthenticated. Please signup!"))
      end
          return  HTTP.Response(200,string("<b>verify !</b>"))
      end

  return myfunction(middleware(request,HTTP)...)

  end)()

)
```


---

# Agent Instructions: 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://neomatrixcode.gitbook.io/merly/guide/middleware.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.
