.
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 { composeMiddleware } from "@/contexts/common/infrastructure/express";
|
||||
import { generateExpressError } from "@/contexts/common/infrastructure/express/ExpressErrorResponse";
|
||||
import { composeMiddleware, generateExpressError } from "@/contexts/common/infrastructure/express";
|
||||
import Express from "express";
|
||||
import httpStatus from "http-status";
|
||||
import passport from "passport";
|
||||
@ -11,17 +10,15 @@ export const isUser = composeMiddleware([
|
||||
}),
|
||||
(req: Express.Request, res: Express.Response, next: Express.NextFunction) => {
|
||||
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 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 passport from "passport";
|
||||
import { createLoginController } from "./controllers";
|
||||
import { createIdentityController } from "./controllers/identity";
|
||||
import { isUser } from "./passport";
|
||||
|
||||
export const AuthRouter = (appRouter: Express.Router) => {
|
||||
@ -23,5 +24,12 @@ export const AuthRouter = (appRouter: Express.Router) => {
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
export * from "./ExpressController";
|
||||
export * from "./ExpressErrorResponse";
|
||||
export * from "./PassportStrategyController";
|
||||
export * from "./middlewares";
|
||||
|
||||
@ -1,17 +1,11 @@
|
||||
import {
|
||||
IUseCaseError,
|
||||
UseCaseError,
|
||||
} from "@/contexts/common/application/useCases";
|
||||
import { IUseCaseError, UseCaseError } from "@/contexts/common/application/useCases";
|
||||
import { ExpressController } from "@/contexts/common/infrastructure/express";
|
||||
import { GetUserUseCase } from "@/contexts/users/application";
|
||||
import { User } from "@/contexts/users/domain/entities/User";
|
||||
import { IGetUserResponse_DTO, ensureIdIsValid } from "@shared/contexts";
|
||||
|
||||
import { IServerError } from "@/contexts/common/domain/errors";
|
||||
import {
|
||||
IInfrastructureError,
|
||||
InfrastructureError,
|
||||
} from "@/contexts/common/infrastructure";
|
||||
import { IInfrastructureError, InfrastructureError } from "@/contexts/common/infrastructure";
|
||||
import { IUserContext } from "../../../User.context";
|
||||
import { IGetUserPresenter } from "./presenter";
|
||||
|
||||
@ -25,7 +19,7 @@ export class GetUserController extends ExpressController {
|
||||
useCase: GetUserUseCase;
|
||||
presenter: IGetUserPresenter;
|
||||
},
|
||||
context: IUserContext,
|
||||
context: IUserContext
|
||||
) {
|
||||
super();
|
||||
|
||||
@ -45,7 +39,7 @@ export class GetUserController extends ExpressController {
|
||||
const infraError = InfrastructureError.create(
|
||||
InfrastructureError.INVALID_INPUT_DATA,
|
||||
errorMessage,
|
||||
userIdOrError.error,
|
||||
userIdOrError.error
|
||||
);
|
||||
return this.invalidInputError(errorMessage, infraError);
|
||||
}
|
||||
@ -61,9 +55,7 @@ export class GetUserController extends ExpressController {
|
||||
|
||||
const user = <User>result.object;
|
||||
|
||||
return this.ok<IGetUserResponse_DTO>(
|
||||
this.presenter.map(user, this.context),
|
||||
);
|
||||
return this.ok<IGetUserResponse_DTO>(this.presenter.map(user, this.context));
|
||||
} catch (e: unknown) {
|
||||
return this.fail(e as IServerError);
|
||||
}
|
||||
@ -80,7 +72,7 @@ export class GetUserController extends ExpressController {
|
||||
infraError = InfrastructureError.create(
|
||||
InfrastructureError.RESOURCE_NOT_FOUND_ERROR,
|
||||
errorMessage,
|
||||
error,
|
||||
error
|
||||
);
|
||||
|
||||
return this.notFoundError(errorMessage, infraError);
|
||||
@ -92,7 +84,7 @@ export class GetUserController extends ExpressController {
|
||||
infraError = InfrastructureError.create(
|
||||
InfrastructureError.UNEXCEPTED_ERROR,
|
||||
errorMessage,
|
||||
error,
|
||||
error
|
||||
);
|
||||
return this.internalServerError(errorMessage, infraError);
|
||||
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_Response.dto";
|
||||
|
||||
Loading…
Reference in New Issue
Block a user