91 lines
2.6 KiB
TypeScript
91 lines
2.6 KiB
TypeScript
import { validateRequestDTO } from "@common/presentation";
|
|
import { checkTabContext, checkUser } from "@contexts/auth/infraestructure";
|
|
import {
|
|
loginController,
|
|
logoutController,
|
|
refreshTokenController,
|
|
registerController,
|
|
} from "@contexts/auth/presentation/controllers";
|
|
import {
|
|
LoginUserSchema,
|
|
RefreshTokenSchema,
|
|
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", validateRequestDTO(RegisterUserSchema), (req, res, next) => {
|
|
registerController().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",
|
|
validateRequestDTO(LoginUserSchema),
|
|
checkTabContext,
|
|
(req: Request, res: Response, next: NextFunction) => {
|
|
loginController().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",
|
|
checkTabContext,
|
|
checkUser,
|
|
(req: Request, res: Response, next: NextFunction) => {
|
|
logoutController().execute(req, res, next);
|
|
}
|
|
);
|
|
|
|
authRoutes.post(
|
|
"/refresh",
|
|
validateRequestDTO(RefreshTokenSchema),
|
|
checkTabContext,
|
|
(req: Request, res: Response, next: NextFunction) => {
|
|
refreshTokenController().execute(req, res, next);
|
|
}
|
|
);
|
|
|
|
appRouter.use("/auth", authRoutes);
|
|
};
|