Spaces:
Sleeping
Sleeping
Download src/modules/auth/presentation/http/controllers/AuthController.ts from fouadkhalid123/asd: direct link, hf CLI and curl.
- Browser
- Download file 2.55 kB
-
https://huggingface.co/spaces/fouadkhalid123/asd/resolve/main/src/modules/auth/presentation/http/controllers/AuthController.ts
- Command line
-
hf download hf://spaces/fouadkhalid123/asd/src/modules/auth/presentation/http/controllers/AuthController.ts
-
curl -L -o AuthController.ts https://huggingface.co/spaces/fouadkhalid123/asd/resolve/main/src/modules/auth/presentation/http/controllers/AuthController.ts
2.55 kB
| 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, | |
| }); | |
| } | |
| } | |