From 8a428e9e0b2cbe11d130a01111729b4945bab59f Mon Sep 17 00:00:00 2001 From: Igor Antonov Date: Tue, 20 Feb 2024 19:12:43 +0300 Subject: [PATCH] =?UTF-8?q?8.17.=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=20=D1=81=D1=86=D0=B5=D0=BD=D0=B0=D1=80=D0=B8=D0=B9=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D0=B0=D0=B2?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Фронтенду необходима возможность проверять актуальность JWT-токена и если токен актуален, возвращать информацию о пользователе. Сделать такой ресурс несложно. Добавим обработчик для маршрута `GET /login` в `UserController`. --- src/shared/modules/user/user.controller.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/shared/modules/user/user.controller.ts b/src/shared/modules/user/user.controller.ts index 311a3b4..13c4291 100644 --- a/src/shared/modules/user/user.controller.ts +++ b/src/shared/modules/user/user.controller.ts @@ -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( @@ -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)); + } }