76 lines
2.6 KiB
TypeScript
76 lines
2.6 KiB
TypeScript
import { validateRequest } from "@common/presentation";
|
|
import { validateTabContextHeader, validateUser } from "@contexts/auth/presentation";
|
|
import { createLoginController } from "@contexts/auth/presentation/controllers";
|
|
import { createLogoutController } from "@contexts/auth/presentation/controllers/logout/logout.controller";
|
|
import { createRegisterController } from "@contexts/auth/presentation/controllers/register/register.controller";
|
|
import { LoginUserSchema, RegisterUserSchema } from "@contexts/auth/presentation/dto";
|
|
import { NextFunction, Request, Response, Router } from "express";
|
|
|
|
export const authRouter = (appRouter: Router) => {
|
|
const authRoutes: Router = Router({ mergeParams: true });
|
|
|
|
/**
|
|
* @api {post} /api/auth/register Register a new user
|
|
* @apiName RegisterUser
|
|
* @apiGroup Authentication
|
|
* @apiVersion 1.0.0
|
|
*
|
|
* @apiBody {String} username User's unique username.
|
|
* @apiBody {String} email User's email address.
|
|
* @apiBody {String} password User's password (minimum 8 characters).
|
|
*
|
|
* @apiSuccess (201) {String} userId The unique ID of the created user.
|
|
*
|
|
* @apiError (400) {String} message Error message.
|
|
*/
|
|
authRoutes.post("/register", validateRequest(RegisterUserSchema), (req, res, next) => {
|
|
createRegisterController().execute(req, res, next);
|
|
});
|
|
|
|
/**
|
|
* @api {post} /api/auth/login Authenticate a user
|
|
* @apiName LoginUser
|
|
* @apiGroup Authentication
|
|
* @apiVersion 1.0.0
|
|
*
|
|
* @apiHeader {String} Tab ID (x-tab-id)
|
|
* @apiBody {String} email User's email address.
|
|
* @apiBody {String} password User's password.
|
|
*
|
|
* @apiSuccess (200) {String} token JWT authentication token.
|
|
* @apiSuccess (200) {String} userId The unique ID of the authenticated user.
|
|
*
|
|
* @apiError (401) {String} message Invalid email or password.
|
|
*/
|
|
authRoutes.post(
|
|
"/login",
|
|
validateRequest(LoginUserSchema),
|
|
validateTabContextHeader,
|
|
(req: Request, res: Response, next: NextFunction) => {
|
|
createLoginController().execute(req, res, next);
|
|
}
|
|
);
|
|
|
|
/**
|
|
* @api {post} /api/auth/logout Logout user
|
|
* @apiName LogoutUser
|
|
* @apiGroup Authentication
|
|
* @apiVersion 1.0.0
|
|
*
|
|
* @apiHeader {String} Tab ID (x-tab-id)
|
|
* @apiHeader {String} Authorization Bearer token.
|
|
*
|
|
* @apiSuccess (200) {String} message Success message.
|
|
*/
|
|
authRoutes.post(
|
|
"/logout",
|
|
validateUser,
|
|
validateTabContextHeader,
|
|
(req: Request, res: Response, next: NextFunction) => {
|
|
createLogoutController().execute(req, res, next);
|
|
}
|
|
);
|
|
|
|
appRouter.use("/auth", authRoutes);
|
|
};
|