.
This commit is contained in:
parent
d0ccc20a1c
commit
cdb6e7de99
@ -0,0 +1,46 @@
|
|||||||
|
import { AuthUser } from "@/contexts/auth/domain";
|
||||||
|
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 { IAuthContext } from "../../../Auth.context";
|
||||||
|
import { IIdentityPresenter, IIdentityUser } from "./presenter/Identity.presenter";
|
||||||
|
|
||||||
|
export class IdentityController extends ExpressController {
|
||||||
|
private presenter: IIdentityPresenter;
|
||||||
|
private context: IAuthContext;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
props: {
|
||||||
|
presenter: IIdentityPresenter;
|
||||||
|
},
|
||||||
|
context: IAuthContext
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
const { presenter } = props;
|
||||||
|
this.presenter = presenter;
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
async executeImpl() {
|
||||||
|
try {
|
||||||
|
const user = <AuthUser>this.req.user;
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
const errorMessage = "Unexpected missing user data";
|
||||||
|
const infraError = InfrastructureError.create(
|
||||||
|
InfrastructureError.UNEXCEPTED_ERROR,
|
||||||
|
errorMessage
|
||||||
|
);
|
||||||
|
return this.internalServerError(errorMessage, infraError);
|
||||||
|
}
|
||||||
|
|
||||||
|
const identityUser: IIdentityUser = user;
|
||||||
|
|
||||||
|
return this.ok<IIdentity_Response_DTO>(this.presenter.map(identityUser, this.context));
|
||||||
|
} catch (e: unknown) {
|
||||||
|
return this.fail(e as IServerError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
import { IAuthContext } from "../../../Auth.context";
|
||||||
|
import { IdentityController } from "./Identity.controller";
|
||||||
|
import { identityPresenter } from "./presenter";
|
||||||
|
|
||||||
|
export const createIdentityController = (context: IAuthContext) => {
|
||||||
|
return new IdentityController(
|
||||||
|
{
|
||||||
|
presenter: identityPresenter,
|
||||||
|
},
|
||||||
|
context
|
||||||
|
);
|
||||||
|
};
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
import { IAuthUser } from "@/contexts/auth/domain";
|
||||||
|
import { IAuthContext } from "@/contexts/auth/infrastructure/Auth.context";
|
||||||
|
import { IIdentity_Response_DTO } from "@shared/contexts";
|
||||||
|
|
||||||
|
export interface IIdentityUser extends IAuthUser {}
|
||||||
|
|
||||||
|
export interface IIdentityPresenter {
|
||||||
|
map: (user: IIdentityUser, context: IAuthContext) => IIdentity_Response_DTO;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const identityPresenter: IIdentityPresenter = {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
map: (user: IIdentityUser, context: IAuthContext): IIdentity_Response_DTO => {
|
||||||
|
return {
|
||||||
|
id: user.id.toString(),
|
||||||
|
name: user.name.toString(),
|
||||||
|
email: user.email.toString(),
|
||||||
|
roles: user.getRoles()?.map((rol) => rol.toString()),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
@ -0,0 +1 @@
|
|||||||
|
export * from "./Identity.presenter";
|
||||||
@ -1,6 +1,5 @@
|
|||||||
import { AuthUser } from "@/contexts/auth/domain";
|
import { AuthUser } from "@/contexts/auth/domain";
|
||||||
import { composeMiddleware } from "@/contexts/common/infrastructure/express";
|
import { composeMiddleware, generateExpressError } from "@/contexts/common/infrastructure/express";
|
||||||
import { generateExpressError } from "@/contexts/common/infrastructure/express/ExpressErrorResponse";
|
|
||||||
import Express from "express";
|
import Express from "express";
|
||||||
import httpStatus from "http-status";
|
import httpStatus from "http-status";
|
||||||
import passport from "passport";
|
import passport from "passport";
|
||||||
@ -11,17 +10,15 @@ export const isUser = composeMiddleware([
|
|||||||
}),
|
}),
|
||||||
(req: Express.Request, res: Express.Response, next: Express.NextFunction) => {
|
(req: Express.Request, res: Express.Response, next: Express.NextFunction) => {
|
||||||
if (req.isAuthenticated()) {
|
if (req.isAuthenticated()) {
|
||||||
req.authInfo = <AuthUser>req.user;
|
console.log(<AuthUser>req.user);
|
||||||
|
/*const user = <AuthUser>req.user;
|
||||||
|
if (!user.isUser) {
|
||||||
|
return generateExpressError(req, res, httpStatus.UNAUTHORIZED);
|
||||||
|
}*/
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
return generateExpressError(req, res, httpStatus.UNAUTHORIZED);
|
return generateExpressError(req, res, httpStatus.UNAUTHORIZED);
|
||||||
|
|
||||||
/*const user = <AuthUser>req.user;
|
|
||||||
if (!user.isUser) {
|
|
||||||
return generateExpressError(req, res, httpStatus.UNAUTHORIZED);
|
|
||||||
}
|
|
||||||
next();*/
|
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import Express from "express";
|
import Express from "express";
|
||||||
import passport from "passport";
|
import passport from "passport";
|
||||||
import { createLoginController } from "./controllers";
|
import { createLoginController } from "./controllers";
|
||||||
|
import { createIdentityController } from "./controllers/identity";
|
||||||
import { isUser } from "./passport";
|
import { isUser } from "./passport";
|
||||||
|
|
||||||
export const AuthRouter = (appRouter: Express.Router) => {
|
export const AuthRouter = (appRouter: Express.Router) => {
|
||||||
@ -23,5 +24,12 @@ export const AuthRouter = (appRouter: Express.Router) => {
|
|||||||
|
|
||||||
authRoutes.post("/register");
|
authRoutes.post("/register");
|
||||||
|
|
||||||
|
authRoutes.get(
|
||||||
|
"/identity",
|
||||||
|
isUser,
|
||||||
|
(req: Express.Request, res: Express.Response, next: Express.NextFunction) =>
|
||||||
|
createIdentityController(res.locals["context"]).execute(req, res, next)
|
||||||
|
);
|
||||||
|
|
||||||
appRouter.use("/auth", authRoutes);
|
appRouter.use("/auth", authRoutes);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
export * from "./ExpressController";
|
export * from "./ExpressController";
|
||||||
|
export * from "./ExpressErrorResponse";
|
||||||
export * from "./PassportStrategyController";
|
export * from "./PassportStrategyController";
|
||||||
export * from "./middlewares";
|
export * from "./middlewares";
|
||||||
|
|||||||
@ -1,17 +1,11 @@
|
|||||||
import {
|
import { IUseCaseError, UseCaseError } from "@/contexts/common/application/useCases";
|
||||||
IUseCaseError,
|
|
||||||
UseCaseError,
|
|
||||||
} from "@/contexts/common/application/useCases";
|
|
||||||
import { ExpressController } from "@/contexts/common/infrastructure/express";
|
import { ExpressController } from "@/contexts/common/infrastructure/express";
|
||||||
import { GetUserUseCase } from "@/contexts/users/application";
|
import { GetUserUseCase } from "@/contexts/users/application";
|
||||||
import { User } from "@/contexts/users/domain/entities/User";
|
import { User } from "@/contexts/users/domain/entities/User";
|
||||||
import { IGetUserResponse_DTO, ensureIdIsValid } from "@shared/contexts";
|
import { IGetUserResponse_DTO, ensureIdIsValid } from "@shared/contexts";
|
||||||
|
|
||||||
import { IServerError } from "@/contexts/common/domain/errors";
|
import { IServerError } from "@/contexts/common/domain/errors";
|
||||||
import {
|
import { IInfrastructureError, InfrastructureError } from "@/contexts/common/infrastructure";
|
||||||
IInfrastructureError,
|
|
||||||
InfrastructureError,
|
|
||||||
} from "@/contexts/common/infrastructure";
|
|
||||||
import { IUserContext } from "../../../User.context";
|
import { IUserContext } from "../../../User.context";
|
||||||
import { IGetUserPresenter } from "./presenter";
|
import { IGetUserPresenter } from "./presenter";
|
||||||
|
|
||||||
@ -25,7 +19,7 @@ export class GetUserController extends ExpressController {
|
|||||||
useCase: GetUserUseCase;
|
useCase: GetUserUseCase;
|
||||||
presenter: IGetUserPresenter;
|
presenter: IGetUserPresenter;
|
||||||
},
|
},
|
||||||
context: IUserContext,
|
context: IUserContext
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
@ -45,7 +39,7 @@ export class GetUserController extends ExpressController {
|
|||||||
const infraError = InfrastructureError.create(
|
const infraError = InfrastructureError.create(
|
||||||
InfrastructureError.INVALID_INPUT_DATA,
|
InfrastructureError.INVALID_INPUT_DATA,
|
||||||
errorMessage,
|
errorMessage,
|
||||||
userIdOrError.error,
|
userIdOrError.error
|
||||||
);
|
);
|
||||||
return this.invalidInputError(errorMessage, infraError);
|
return this.invalidInputError(errorMessage, infraError);
|
||||||
}
|
}
|
||||||
@ -61,9 +55,7 @@ export class GetUserController extends ExpressController {
|
|||||||
|
|
||||||
const user = <User>result.object;
|
const user = <User>result.object;
|
||||||
|
|
||||||
return this.ok<IGetUserResponse_DTO>(
|
return this.ok<IGetUserResponse_DTO>(this.presenter.map(user, this.context));
|
||||||
this.presenter.map(user, this.context),
|
|
||||||
);
|
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
return this.fail(e as IServerError);
|
return this.fail(e as IServerError);
|
||||||
}
|
}
|
||||||
@ -80,7 +72,7 @@ export class GetUserController extends ExpressController {
|
|||||||
infraError = InfrastructureError.create(
|
infraError = InfrastructureError.create(
|
||||||
InfrastructureError.RESOURCE_NOT_FOUND_ERROR,
|
InfrastructureError.RESOURCE_NOT_FOUND_ERROR,
|
||||||
errorMessage,
|
errorMessage,
|
||||||
error,
|
error
|
||||||
);
|
);
|
||||||
|
|
||||||
return this.notFoundError(errorMessage, infraError);
|
return this.notFoundError(errorMessage, infraError);
|
||||||
@ -92,7 +84,7 @@ export class GetUserController extends ExpressController {
|
|||||||
infraError = InfrastructureError.create(
|
infraError = InfrastructureError.create(
|
||||||
InfrastructureError.UNEXCEPTED_ERROR,
|
InfrastructureError.UNEXCEPTED_ERROR,
|
||||||
errorMessage,
|
errorMessage,
|
||||||
error,
|
error
|
||||||
);
|
);
|
||||||
return this.internalServerError(errorMessage, infraError);
|
return this.internalServerError(errorMessage, infraError);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -0,0 +1,6 @@
|
|||||||
|
export interface IIdentity_Response_DTO {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
roles: string[];
|
||||||
|
}
|
||||||
@ -1,2 +1,3 @@
|
|||||||
|
export * from "./IIdentity_Response.dto";
|
||||||
export * from "./ILogin.dto";
|
export * from "./ILogin.dto";
|
||||||
export * from "./ILogin_Response.dto";
|
export * from "./ILogin_Response.dto";
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user