> For the complete documentation index, see [llms.txt](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/extra-module-1-authentication-authorization/authentication/request-user-decorator.md).

# Request user decorator

An easier and safer way to extract the user from the request.

Notice that we are extracting the <mark style="color:blue;">`user`</mark> directly from the <mark style="color:blue;">`request`</mark>, which in turn is of type <mark style="color:red;">any</mark>. We'll create a decorator to directly access the <mark style="color:blue;">`user`</mark> property from the <mark style="color:blue;">`request`</mark> without having to access it directly, as is already done with <mark style="color:blue;">`@Body()`</mark>, <mark style="color:blue;">`@Param()`</mark>, <mark style="color:blue;">`@Query()`</mark>, etc.

First, create the file <mark style="color:purple;">auth</mark>/<mark style="color:purple;">decorators</mark>/<mark style="color:purple;">user.decorator</mark>. The contents of this file are mainly boilerplate for directly extracting something from the <mark style="color:blue;">`request`</mark>. What we should really notice is the return at the end.

```typescript
export const User = createParamDecorator(
  (data: unknown, context: ExecutionContext) => {
    const request = context.switchToHttp().getRequest<Request>();
    return request.user;
  },
);
```

{% hint style="info" %}
If you prefer, you can give a different name to this decorator, like <mark style="color:blue;">`CurrentUser`</mark> or <mark style="color:blue;">`ActiveUser`</mark>, to prevent importing the <mark style="color:blue;">`User`</mark> **entity** by accident and vice-versa.
{% endhint %}

{% hint style="info" %}
The <mark style="color:blue;">`Request`</mark> type should be imported from <mark style="color:blue;">express</mark>.
{% endhint %}

And we're done, now we can obtain the <mark style="color:blue;">`user`</mark> from the <mark style="color:blue;">`request`</mark> in a better way.

```typescript
login(@User() user) {
  return user;
}
```

<mark style="color:green;">**Commit**</mark> - Creating decorator to extract user from request

However, the <mark style="color:blue;">`user`</mark> itself still has the type <mark style="color:red;">any</mark>. We'll solve this in the next section.
