.
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 { InfrastructureError } from "@/contexts/common/infrastructure";
|
||||||
import { ExpressController } from "@/contexts/common/infrastructure/express";
|
import { ExpressController } from "@/contexts/common/infrastructure/express";
|
||||||
import { IIdentity_Response_DTO } from "@shared/contexts";
|
import { IIdentity_Response_DTO } from "@shared/contexts";
|
||||||
|
import { Request } from "express";
|
||||||
import { IAuthContext } from "../../../Auth.context";
|
import { IAuthContext } from "../../../Auth.context";
|
||||||
import { IIdentityPresenter, IIdentityUser } from "./presenter/Identity.presenter";
|
import { IIdentityPresenter, IIdentityUser } from "./presenter/Identity.presenter";
|
||||||
|
|
||||||
|
interface AuthenticatedRequest extends Request {
|
||||||
|
user?: AuthUser;
|
||||||
|
}
|
||||||
|
|
||||||
export class IdentityController extends ExpressController {
|
export class IdentityController extends ExpressController {
|
||||||
private presenter: IIdentityPresenter;
|
private presenter: IIdentityPresenter;
|
||||||
private context: IAuthContext;
|
private context: IAuthContext;
|
||||||
@ -25,8 +30,7 @@ export class IdentityController extends ExpressController {
|
|||||||
|
|
||||||
async executeImpl() {
|
async executeImpl() {
|
||||||
try {
|
try {
|
||||||
const req = this.req as Express.AuthenticatedRequest;
|
const user = <AuthUser>this.req.user;
|
||||||
const user = <AuthUser>req.user;
|
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
const errorMessage = "Unexpected missing user data";
|
const errorMessage = "Unexpected missing user data";
|
||||||
|
|||||||
@ -4,10 +4,15 @@ import { IServerError } from "@/contexts/common/domain/errors";
|
|||||||
import { InfrastructureError } from "@/contexts/common/infrastructure";
|
import { InfrastructureError } from "@/contexts/common/infrastructure";
|
||||||
import { ExpressController } from "@/contexts/common/infrastructure/express";
|
import { ExpressController } from "@/contexts/common/infrastructure/express";
|
||||||
import { ILogin_Response_DTO } from "@shared/contexts";
|
import { ILogin_Response_DTO } from "@shared/contexts";
|
||||||
|
import { Request } from "express";
|
||||||
import JWT from "jsonwebtoken";
|
import JWT from "jsonwebtoken";
|
||||||
import { IAuthContext } from "../../../Auth.context";
|
import { IAuthContext } from "../../../Auth.context";
|
||||||
import { ILoginPresenter, ILoginUser } from "./presenter";
|
import { ILoginPresenter, ILoginUser } from "./presenter";
|
||||||
|
|
||||||
|
interface AuthenticatedRequest extends Request {
|
||||||
|
user?: AuthUser;
|
||||||
|
}
|
||||||
|
|
||||||
export class LoginController extends ExpressController {
|
export class LoginController extends ExpressController {
|
||||||
private presenter: ILoginPresenter;
|
private presenter: ILoginPresenter;
|
||||||
private context: IAuthContext;
|
private context: IAuthContext;
|
||||||
@ -27,8 +32,7 @@ export class LoginController extends ExpressController {
|
|||||||
|
|
||||||
async executeImpl() {
|
async executeImpl() {
|
||||||
try {
|
try {
|
||||||
const req = this.req as Express.AuthenticatedRequest;
|
const user = <AuthUser>this.req.user;
|
||||||
const user = <AuthUser>req.user;
|
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
const errorMessage = "Unexpected missing user data";
|
const errorMessage = "Unexpected missing user data";
|
||||||
|
|||||||
@ -5,6 +5,10 @@ import * as express from "express";
|
|||||||
import httpStatus from "http-status";
|
import httpStatus from "http-status";
|
||||||
import passport from "passport";
|
import passport from "passport";
|
||||||
|
|
||||||
|
interface AuthenticatedRequest extends express.Request {
|
||||||
|
user?: AuthUser;
|
||||||
|
}
|
||||||
|
|
||||||
export const checkUser = composeMiddleware([
|
export const checkUser = composeMiddleware([
|
||||||
passport.authenticate("local-jwt", {
|
passport.authenticate("local-jwt", {
|
||||||
session: false,
|
session: false,
|
||||||
@ -20,7 +24,7 @@ export const checkUser = composeMiddleware([
|
|||||||
export const checkisAdmin = composeMiddleware([
|
export const checkisAdmin = composeMiddleware([
|
||||||
checkUser,
|
checkUser,
|
||||||
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
(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 user = <AuthUser>_req.user;
|
||||||
|
|
||||||
if (!user || !user.isAdmin) {
|
if (!user || !user.isAdmin) {
|
||||||
@ -33,7 +37,7 @@ export const checkisAdmin = composeMiddleware([
|
|||||||
export const checkAdminOrSelf = composeMiddleware([
|
export const checkAdminOrSelf = composeMiddleware([
|
||||||
checkUser,
|
checkUser,
|
||||||
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
(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 user = <AuthUser>_req.user;
|
||||||
|
|
||||||
const { userId } = req.params;
|
const { userId } = req.params;
|
||||||
|
|||||||
@ -18,6 +18,11 @@ import {
|
|||||||
import { ICatalogContext } from "../../..";
|
import { ICatalogContext } from "../../..";
|
||||||
import { IListArticlesPresenter } from "./presenter";
|
import { IListArticlesPresenter } from "./presenter";
|
||||||
|
|
||||||
|
import { Request } from "express";
|
||||||
|
interface AuthenticatedRequest extends Request {
|
||||||
|
user?: AuthUser;
|
||||||
|
}
|
||||||
|
|
||||||
export class ListArticlesController extends ExpressController {
|
export class ListArticlesController extends ExpressController {
|
||||||
private useCase: ListArticlesUseCase;
|
private useCase: ListArticlesUseCase;
|
||||||
private presenter: IListArticlesPresenter;
|
private presenter: IListArticlesPresenter;
|
||||||
@ -51,7 +56,7 @@ export class ListArticlesController extends ExpressController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async executeImpl() {
|
async executeImpl() {
|
||||||
const req = this.req as Express.AuthenticatedRequest;
|
const req = this.req as AuthenticatedRequest;
|
||||||
const { language = Language.createDefaultCode() } = <AuthUser>req.user;
|
const { language = Language.createDefaultCode() } = <AuthUser>req.user;
|
||||||
|
|
||||||
const queryOrError = this.validateQuery({
|
const queryOrError = this.validateQuery({
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { IError_Response_DTO } from "@shared/contexts";
|
import { IError_Response_DTO } from "@shared/contexts";
|
||||||
import * as express from "express";
|
import Express from "express";
|
||||||
import httpStatus from "http-status";
|
import httpStatus from "http-status";
|
||||||
import { URL } from "url";
|
import { URL } from "url";
|
||||||
import { IServerError } from "../../domain/errors";
|
import { IServerError } from "../../domain/errors";
|
||||||
@ -8,16 +8,16 @@ import { InfrastructureError } from "../InfrastructureError";
|
|||||||
import { generateExpressError } from "./ExpressErrorResponse";
|
import { generateExpressError } from "./ExpressErrorResponse";
|
||||||
|
|
||||||
export abstract class ExpressController implements IController {
|
export abstract class ExpressController implements IController {
|
||||||
protected req: express.Request;
|
protected req: Express.Request;
|
||||||
protected res: express.Response;
|
protected res: Express.Response;
|
||||||
protected next: express.NextFunction;
|
protected next: Express.NextFunction;
|
||||||
|
|
||||||
protected serverURL: string = "";
|
protected serverURL: string = "";
|
||||||
protected file: any;
|
protected file: any;
|
||||||
|
|
||||||
protected abstract executeImpl(): Promise<void | 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.req = req;
|
||||||
this.res = res;
|
this.res = res;
|
||||||
this.next = next;
|
this.next = next;
|
||||||
@ -109,7 +109,7 @@ export abstract class ExpressController implements IController {
|
|||||||
return this._errorResponse(httpStatus.SERVICE_UNAVAILABLE, message);
|
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();
|
return this.res.status(statusCode).json(jsonPayload).send();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,7 +117,7 @@ export abstract class ExpressController implements IController {
|
|||||||
statusCode: number,
|
statusCode: number,
|
||||||
message?: string,
|
message?: string,
|
||||||
error?: Error | InfrastructureError
|
error?: Error | InfrastructureError
|
||||||
): express.Response<IError_Response_DTO> {
|
): Express.Response<IError_Response_DTO> {
|
||||||
return generateExpressError(this.req, this.res, statusCode, message, error);
|
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 { AuthUser } from "@/contexts/auth/domain";
|
||||||
|
import { IUseCaseError, UseCaseError } from "@/contexts/common/application/useCases";
|
||||||
import { IServerError } from "@/contexts/common/domain/errors";
|
import { IServerError } from "@/contexts/common/domain/errors";
|
||||||
import { IInfrastructureError, InfrastructureError } from "@/contexts/common/infrastructure";
|
import { IInfrastructureError, InfrastructureError } from "@/contexts/common/infrastructure";
|
||||||
|
import { ExpressController } from "@/contexts/common/infrastructure/express";
|
||||||
import { GetProfileUseCase } from "@/contexts/profile/application/GetProfile.useCase";
|
import { GetProfileUseCase } from "@/contexts/profile/application/GetProfile.useCase";
|
||||||
|
import { IGetProfileResponse_DTO } from "@shared/contexts";
|
||||||
import { IProfileContext } from "../../../Profile.context";
|
import { IProfileContext } from "../../../Profile.context";
|
||||||
import { IGetProfilePresenter } from "./presenter";
|
import { IGetProfilePresenter } from "./presenter";
|
||||||
|
|
||||||
|
import { Request } from "express";
|
||||||
|
|
||||||
|
interface AuthenticatedRequest extends Request {
|
||||||
|
user?: AuthUser;
|
||||||
|
}
|
||||||
|
|
||||||
export class GetProfileController extends ExpressController {
|
export class GetProfileController extends ExpressController {
|
||||||
private useCase: GetProfileUseCase;
|
private useCase: GetProfileUseCase;
|
||||||
private presenter: IGetProfilePresenter;
|
private presenter: IGetProfilePresenter;
|
||||||
@ -30,7 +35,7 @@ export class GetProfileController extends ExpressController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async executeImpl(): Promise<any> {
|
async executeImpl(): Promise<any> {
|
||||||
const req = this.req as Express.AuthenticatedRequest;
|
const req = this.req as AuthenticatedRequest;
|
||||||
const user = <AuthUser>req.user;
|
const user = <AuthUser>req.user;
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
|||||||
@ -33,8 +33,7 @@ export class UpdateProfileController extends ExpressController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async executeImpl() {
|
async executeImpl() {
|
||||||
const req = this.req as Express.AuthenticatedRequest;
|
const user = <User>this.req.user;
|
||||||
const user = <User>req.user;
|
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
const errorMessage = "Unexpected missing Profile data";
|
const errorMessage = "Unexpected missing Profile data";
|
||||||
|
|||||||
@ -4,12 +4,16 @@ import * as express from "express";
|
|||||||
import { registerDealerRepository } from "../../Dealer.repository";
|
import { registerDealerRepository } from "../../Dealer.repository";
|
||||||
import { ISalesContext } from "../../Sales.context";
|
import { ISalesContext } from "../../Sales.context";
|
||||||
|
|
||||||
|
interface AuthenticatedRequest extends express.Request {
|
||||||
|
user?: AuthUser;
|
||||||
|
}
|
||||||
|
|
||||||
export const getDealerMiddleware = async (
|
export const getDealerMiddleware = async (
|
||||||
req: express.Request,
|
req: express.Request,
|
||||||
res: express.Response,
|
res: express.Response,
|
||||||
next: express.NextFunction
|
next: express.NextFunction
|
||||||
) => {
|
) => {
|
||||||
const _req = req as Express.AuthenticatedRequest;
|
const _req = req as AuthenticatedRequest;
|
||||||
const user = <AuthUser>_req.user;
|
const user = <AuthUser>_req.user;
|
||||||
const context: ISalesContext = res.locals.context;
|
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) => {
|
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) {
|
_req.logout(function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return res.status(500).json();
|
return res.status(500).json();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user