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 { 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 { 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 { 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 { const user = (req as any).user; res.status(200).json({ userId: (req as any).userId, organizationId: user?.claims?.organizationId, storeId: user?.claims?.storeId, }); } }