From 8700c232b48adfc7e561a340908204b7f35ac296 Mon Sep 17 00:00:00 2001 From: David Arranz Date: Sun, 8 Sep 2024 14:38:05 +0200 Subject: [PATCH] . --- .../controllers/identity/Identity.controller.ts | 8 ++++++-- .../express/controllers/login/Login.controller.ts | 8 ++++++-- .../express/passport/authMiddleware.ts | 8 ++++++-- .../listArticles/ListArticlesController.ts | 7 ++++++- .../infrastructure/express/ExpressController.ts | 14 +++++++------- .../getProfile/GetProfile.controller.ts | 15 ++++++++++----- .../updateProfile/UpdateProfile.controller.ts | 3 +-- .../express/middlewares/dealerMiddleware.ts | 6 +++++- .../express/api/routes/auth.routes.ts | 2 +- 9 files changed, 48 insertions(+), 23 deletions(-) diff --git a/server/src/contexts/auth/infrastructure/express/controllers/identity/Identity.controller.ts b/server/src/contexts/auth/infrastructure/express/controllers/identity/Identity.controller.ts index c3e4903..8ebb628 100644 --- a/server/src/contexts/auth/infrastructure/express/controllers/identity/Identity.controller.ts +++ b/server/src/contexts/auth/infrastructure/express/controllers/identity/Identity.controller.ts @@ -3,9 +3,14 @@ import { IServerError } from "@/contexts/common/domain/errors"; import { InfrastructureError } from "@/contexts/common/infrastructure"; import { ExpressController } from "@/contexts/common/infrastructure/express"; import { IIdentity_Response_DTO } from "@shared/contexts"; +import { Request } from "express"; import { IAuthContext } from "../../../Auth.context"; import { IIdentityPresenter, IIdentityUser } from "./presenter/Identity.presenter"; +interface AuthenticatedRequest extends Request { + user?: AuthUser; +} + export class IdentityController extends ExpressController { private presenter: IIdentityPresenter; private context: IAuthContext; @@ -25,8 +30,7 @@ export class IdentityController extends ExpressController { async executeImpl() { try { - const req = this.req as Express.AuthenticatedRequest; - const user = req.user; + const user = this.req.user; if (!user) { const errorMessage = "Unexpected missing user data"; diff --git a/server/src/contexts/auth/infrastructure/express/controllers/login/Login.controller.ts b/server/src/contexts/auth/infrastructure/express/controllers/login/Login.controller.ts index 72dd4b3..6b2a32d 100644 --- a/server/src/contexts/auth/infrastructure/express/controllers/login/Login.controller.ts +++ b/server/src/contexts/auth/infrastructure/express/controllers/login/Login.controller.ts @@ -4,10 +4,15 @@ import { IServerError } from "@/contexts/common/domain/errors"; import { InfrastructureError } from "@/contexts/common/infrastructure"; import { ExpressController } from "@/contexts/common/infrastructure/express"; import { ILogin_Response_DTO } from "@shared/contexts"; +import { Request } from "express"; import JWT from "jsonwebtoken"; import { IAuthContext } from "../../../Auth.context"; import { ILoginPresenter, ILoginUser } from "./presenter"; +interface AuthenticatedRequest extends Request { + user?: AuthUser; +} + export class LoginController extends ExpressController { private presenter: ILoginPresenter; private context: IAuthContext; @@ -27,8 +32,7 @@ export class LoginController extends ExpressController { async executeImpl() { try { - const req = this.req as Express.AuthenticatedRequest; - const user = req.user; + const user = this.req.user; if (!user) { const errorMessage = "Unexpected missing user data"; diff --git a/server/src/contexts/auth/infrastructure/express/passport/authMiddleware.ts b/server/src/contexts/auth/infrastructure/express/passport/authMiddleware.ts index 78362b7..afc8106 100644 --- a/server/src/contexts/auth/infrastructure/express/passport/authMiddleware.ts +++ b/server/src/contexts/auth/infrastructure/express/passport/authMiddleware.ts @@ -5,6 +5,10 @@ import * as express from "express"; import httpStatus from "http-status"; import passport from "passport"; +interface AuthenticatedRequest extends express.Request { + user?: AuthUser; +} + export const checkUser = composeMiddleware([ passport.authenticate("local-jwt", { session: false, @@ -20,7 +24,7 @@ export const checkUser = composeMiddleware([ export const checkisAdmin = composeMiddleware([ checkUser, (req: express.Request, res: express.Response, next: express.NextFunction) => { - const _req = req as Express.AuthenticatedRequest; + const _req = req as AuthenticatedRequest; const user = _req.user; if (!user || !user.isAdmin) { @@ -33,7 +37,7 @@ export const checkisAdmin = composeMiddleware([ export const checkAdminOrSelf = composeMiddleware([ checkUser, (req: express.Request, res: express.Response, next: express.NextFunction) => { - const _req = req as Express.AuthenticatedRequest; + const _req = req as AuthenticatedRequest; const user = _req.user; const { userId } = req.params; diff --git a/server/src/contexts/catalog/infrastructure/express/controllers/listArticles/ListArticlesController.ts b/server/src/contexts/catalog/infrastructure/express/controllers/listArticles/ListArticlesController.ts index 8ea3cc6..78c6947 100644 --- a/server/src/contexts/catalog/infrastructure/express/controllers/listArticles/ListArticlesController.ts +++ b/server/src/contexts/catalog/infrastructure/express/controllers/listArticles/ListArticlesController.ts @@ -18,6 +18,11 @@ import { import { ICatalogContext } from "../../.."; import { IListArticlesPresenter } from "./presenter"; +import { Request } from "express"; +interface AuthenticatedRequest extends Request { + user?: AuthUser; +} + export class ListArticlesController extends ExpressController { private useCase: ListArticlesUseCase; private presenter: IListArticlesPresenter; @@ -51,7 +56,7 @@ export class ListArticlesController extends ExpressController { } async executeImpl() { - const req = this.req as Express.AuthenticatedRequest; + const req = this.req as AuthenticatedRequest; const { language = Language.createDefaultCode() } = req.user; const queryOrError = this.validateQuery({ diff --git a/server/src/contexts/common/infrastructure/express/ExpressController.ts b/server/src/contexts/common/infrastructure/express/ExpressController.ts index c1b6b8d..7636366 100644 --- a/server/src/contexts/common/infrastructure/express/ExpressController.ts +++ b/server/src/contexts/common/infrastructure/express/ExpressController.ts @@ -1,5 +1,5 @@ import { IError_Response_DTO } from "@shared/contexts"; -import * as express from "express"; +import Express from "express"; import httpStatus from "http-status"; import { URL } from "url"; import { IServerError } from "../../domain/errors"; @@ -8,16 +8,16 @@ import { InfrastructureError } from "../InfrastructureError"; import { generateExpressError } from "./ExpressErrorResponse"; export abstract class ExpressController implements IController { - protected req: express.Request; - protected res: express.Response; - protected next: express.NextFunction; + protected req: Express.Request; + protected res: Express.Response; + protected next: Express.NextFunction; protected serverURL: string = ""; protected file: any; protected abstract executeImpl(): Promise; - public execute(req: express.Request, res: express.Response, next: express.NextFunction): void { + public execute(req: Express.Request, res: Express.Response, next: Express.NextFunction): void { this.req = req; this.res = res; this.next = next; @@ -109,7 +109,7 @@ export abstract class ExpressController implements IController { return this._errorResponse(httpStatus.SERVICE_UNAVAILABLE, message); } - private _jsonResponse(statusCode: number, jsonPayload: any): express.Response { + private _jsonResponse(statusCode: number, jsonPayload: any): Express.Response { return this.res.status(statusCode).json(jsonPayload).send(); } @@ -117,7 +117,7 @@ export abstract class ExpressController implements IController { statusCode: number, message?: string, error?: Error | InfrastructureError - ): express.Response { + ): Express.Response { return generateExpressError(this.req, this.res, statusCode, message, error); } diff --git a/server/src/contexts/profile/infrastructure/express/controllers/getProfile/GetProfile.controller.ts b/server/src/contexts/profile/infrastructure/express/controllers/getProfile/GetProfile.controller.ts index 7b14ece..51eb634 100644 --- a/server/src/contexts/profile/infrastructure/express/controllers/getProfile/GetProfile.controller.ts +++ b/server/src/contexts/profile/infrastructure/express/controllers/getProfile/GetProfile.controller.ts @@ -1,14 +1,19 @@ -import { IUseCaseError, UseCaseError } from "@/contexts/common/application/useCases"; -import { ExpressController } from "@/contexts/common/infrastructure/express"; -import { IGetProfileResponse_DTO } from "@shared/contexts"; - import { AuthUser } from "@/contexts/auth/domain"; +import { IUseCaseError, UseCaseError } from "@/contexts/common/application/useCases"; import { IServerError } from "@/contexts/common/domain/errors"; import { IInfrastructureError, InfrastructureError } from "@/contexts/common/infrastructure"; +import { ExpressController } from "@/contexts/common/infrastructure/express"; import { GetProfileUseCase } from "@/contexts/profile/application/GetProfile.useCase"; +import { IGetProfileResponse_DTO } from "@shared/contexts"; import { IProfileContext } from "../../../Profile.context"; import { IGetProfilePresenter } from "./presenter"; +import { Request } from "express"; + +interface AuthenticatedRequest extends Request { + user?: AuthUser; +} + export class GetProfileController extends ExpressController { private useCase: GetProfileUseCase; private presenter: IGetProfilePresenter; @@ -30,7 +35,7 @@ export class GetProfileController extends ExpressController { } async executeImpl(): Promise { - const req = this.req as Express.AuthenticatedRequest; + const req = this.req as AuthenticatedRequest; const user = req.user; if (!user) { diff --git a/server/src/contexts/profile/infrastructure/express/controllers/updateProfile/UpdateProfile.controller.ts b/server/src/contexts/profile/infrastructure/express/controllers/updateProfile/UpdateProfile.controller.ts index e31a1e9..3c0e12d 100644 --- a/server/src/contexts/profile/infrastructure/express/controllers/updateProfile/UpdateProfile.controller.ts +++ b/server/src/contexts/profile/infrastructure/express/controllers/updateProfile/UpdateProfile.controller.ts @@ -33,8 +33,7 @@ export class UpdateProfileController extends ExpressController { } async executeImpl() { - const req = this.req as Express.AuthenticatedRequest; - const user = req.user; + const user = this.req.user; if (!user) { const errorMessage = "Unexpected missing Profile data"; diff --git a/server/src/contexts/sales/infrastructure/express/middlewares/dealerMiddleware.ts b/server/src/contexts/sales/infrastructure/express/middlewares/dealerMiddleware.ts index 9e65971..1913171 100644 --- a/server/src/contexts/sales/infrastructure/express/middlewares/dealerMiddleware.ts +++ b/server/src/contexts/sales/infrastructure/express/middlewares/dealerMiddleware.ts @@ -4,12 +4,16 @@ import * as express from "express"; import { registerDealerRepository } from "../../Dealer.repository"; import { ISalesContext } from "../../Sales.context"; +interface AuthenticatedRequest extends express.Request { + user?: AuthUser; +} + export const getDealerMiddleware = async ( req: express.Request, res: express.Response, next: express.NextFunction ) => { - const _req = req as Express.AuthenticatedRequest; + const _req = req as AuthenticatedRequest; const user = _req.user; const context: ISalesContext = res.locals.context; diff --git a/server/src/infrastructure/express/api/routes/auth.routes.ts b/server/src/infrastructure/express/api/routes/auth.routes.ts index 326e2da..5c7c23d 100644 --- a/server/src/infrastructure/express/api/routes/auth.routes.ts +++ b/server/src/infrastructure/express/api/routes/auth.routes.ts @@ -16,7 +16,7 @@ export const authRouter = (appRouter: express.Router) => { ); authRoutes.post("/logout", checkUser, (req: express.Request, res: express.Response) => { - const _req = req as Express.AuthenticatedRequest; + const _req = req as Express.Request; _req.logout(function (err) { if (err) { return res.status(500).json();