Spaces:
Sleeping
Sleeping
File size: 2,546 Bytes
7f81110 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | import { Request, Response } from "express";
import { z } from "zod";
import { SignupUseCase } from "../../../application/useCases/SignupUseCase";
import { LoginUseCase } from "../../../application/useCases/LoginUseCase";
import { logoutUseCase } from "../../../application/useCases/LogoutUseCase";
import { UnitOfWorkFactory } from "../../../../../libs/shared/infrastructure/postgres/unitOfWork/UnitOfWorkFactory";
import { TenantPlan } from "../../../../../libs/shared/domain/valueObjects/TenantContext";
import { signupSchema, loginSchema } from "../validation/AuthSchemas";
export class AuthController {
private readonly signupUseCase: SignupUseCase;
private readonly loginUseCase: LoginUseCase;
constructor(uowFactory: UnitOfWorkFactory) {
this.signupUseCase = new SignupUseCase(uowFactory);
this.loginUseCase = new LoginUseCase(uowFactory);
}
async signup(req: Request, res: Response): Promise<void> {
const parsed = signupSchema.safeParse(req.body);
if (!parsed.success) {
res.status(400).json({ error: "Validation failed", details: parsed.error.flatten() });
return;
}
try {
const result = await this.signupUseCase.execute(parsed.data as any);
res.status(201).json(result);
} catch (err: any) {
res.status(err.statusCode ?? 500).json({ error: err.message ?? "Signup failed" });
}
}
async login(req: Request, res: Response): Promise<void> {
const parsed = loginSchema.safeParse(req.body);
if (!parsed.success) {
res.status(400).json({ error: "Validation failed", details: parsed.error.flatten() });
return;
}
try {
const result = await this.loginUseCase.execute(parsed.data);
res.status(200).json(result);
} catch (err: any) {
res.status(err.statusCode ?? 500).json({ error: err.message ?? "Login failed" });
}
}
async logout(req: Request, res: Response): Promise<void> {
const jti = (req as any).jti as string | undefined;
if (jti) {
logoutUseCase(jti);
}
res.status(200).json({ message: "Logged out successfully" });
}
async me(req: Request, res: Response): Promise<void> {
const user = (req as any).user;
res.status(200).json({
userId: (req as any).userId,
organizationId: user?.claims?.organizationId,
storeId: user?.claims?.storeId,
});
}
}
|