.
This commit is contained in:
parent
9c63ba4640
commit
8700c232b4
@ -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 = <AuthUser>req.user;
|
||||
const user = <AuthUser>this.req.user;
|
||||
|
||||
if (!user) {
|
||||
const errorMessage = "Unexpected missing user data";
|
||||
|
||||
@ -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 = <AuthUser>req.user;
|
||||
const user = <AuthUser>this.req.user;
|
||||
|
||||
if (!user) {
|
||||
const errorMessage = "Unexpected missing user data";
|
||||
|
||||
@ -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 = <AuthUser>_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 = <AuthUser>_req.user;
|
||||
|
||||
const { userId } = req.params;
|
||||
|
||||
@ -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() } = <AuthUser>req.user;
|
||||
|
||||
const queryOrError = this.validateQuery({
|
||||
|
||||
@ -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<void | any>;
|
||||
|
||||
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<any> {
|
||||
private _jsonResponse(statusCode: number, jsonPayload: any): Express.Response<any> {
|
||||
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<IError_Response_DTO> {
|
||||
): Express.Response<IError_Response_DTO> {
|
||||
return generateExpressError(this.req, this.res, statusCode, message, error);
|
||||
}
|
||||
|
||||
|
||||
@ -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<any> {
|
||||
const req = this.req as Express.AuthenticatedRequest;
|
||||
const req = this.req as AuthenticatedRequest;
|
||||
const user = <AuthUser>req.user;
|
||||
|
||||
if (!user) {
|
||||
|
||||
@ -33,8 +33,7 @@ export class UpdateProfileController extends ExpressController {
|
||||
}
|
||||
|
||||
async executeImpl() {
|
||||
const req = this.req as Express.AuthenticatedRequest;
|
||||
const user = <User>req.user;
|
||||
const user = <User>this.req.user;
|
||||
|
||||
if (!user) {
|
||||
const errorMessage = "Unexpected missing Profile data";
|
||||
|
||||
@ -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 = <AuthUser>_req.user;
|
||||
const context: ISalesContext = res.locals.context;
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user