Skip to content

Commit

Permalink
8.17. Добавит сценарий проверки авторизации.
Browse files Browse the repository at this point in the history
Фронтенду необходима возможность проверять актуальность JWT-токена и
если токен актуален, возвращать информацию о пользователе. Сделать такой ресурс несложно. Добавим обработчик для маршрута `GET /login` в
`UserController`.
  • Loading branch information
AntonovIgor committed Feb 20, 2024
1 parent 7bd1a3f commit 8a428e9
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/shared/modules/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export class UserController extends BaseController {
new UploadFileMiddleware(this.configService.get('UPLOAD_DIRECTORY'), 'avatar'),
]
});
this.addRoute({
path: '/login',
method: HttpMethod.Get,
handler: this.checkAuthenticate,
});
}

public async create(
Expand Down Expand Up @@ -91,4 +96,18 @@ export class UserController extends BaseController {
filepath: req.file?.path
});
}

public async checkAuthenticate({ tokenPayload: { email }}: Request, res: Response) {
const foundedUser = await this.userService.findByEmail(email);

if (! foundedUser) {
throw new HttpError(
StatusCodes.UNAUTHORIZED,
'Unauthorized',
'UserController'
);
}

this.ok(res, fillDTO(LoggedUserRdo, foundedUser));
}
}

0 comments on commit 8a428e9

Please sign in to comment.