Request user interface

More type safety to the request user.

Let's create the file auth -> interfaces -> request-user.interface. We'll use this interface to define the shape of the user to be appended to the request.

export interface RequestUser {
  readonly id: number;
}

Then, we should return to the AuthService and, at the return of validateLocal(), create a variable with the type of this interface, and then return it.

const requestUser: RequestUser = { id: user.id };
return requestUser;

After that, in the AuthController, give the user extracted from the request, the type of this interface.

login(@User() user: RequestUser)

With this, we're forcing the return of validateLocal() to fulfill the contract of this interface. At the same time, we can safely state that the user extracted from the request also has this type, as it will be populated with the return of validateLocal(). We have a bit more of type safety.

Commit - Applying type safety to request user

Last updated