2024-05-16 11:56:46 +00:00
|
|
|
import { registerMiddleware } from "@/contexts/common/infrastructure/express";
|
2024-05-15 19:56:22 +00:00
|
|
|
import Express from "express";
|
|
|
|
|
import passport from "passport";
|
2024-05-16 11:56:46 +00:00
|
|
|
import { createLoginController } from "./controllers";
|
|
|
|
|
import { isLoggedUser } from "./passport";
|
2024-05-15 19:56:22 +00:00
|
|
|
|
|
|
|
|
/*authRoutes.post(
|
|
|
|
|
"/login",
|
|
|
|
|
passport.authenticate("local-email"),
|
|
|
|
|
|
|
|
|
|
(req: Express.Request, res: Express.Response, next: Express.NextFunction) => {
|
|
|
|
|
console.log("login OK => generate token JWT");
|
|
|
|
|
|
|
|
|
|
// Generar token JWT
|
|
|
|
|
const token = JWT.sign({ userId: req.user?.id }, "clave_secreta", {
|
|
|
|
|
expiresIn: "1h",
|
|
|
|
|
}); // Clave secreta y expiración de 1 hora
|
|
|
|
|
|
|
|
|
|
// Enviar token como respuesta
|
|
|
|
|
res.json({ token });
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
authRoutes.post("/logout", passport.authenticate("local-jwt"));
|
|
|
|
|
|
|
|
|
|
authRoutes.get(
|
|
|
|
|
"/profile",
|
|
|
|
|
passport.authenticate("local-jwt", { session: false }),
|
|
|
|
|
(req: Express.Request, res: Express.Response, next: Express.NextFunction) => {
|
|
|
|
|
res.json({
|
|
|
|
|
message: "You made it to the secure route",
|
|
|
|
|
user: req.user,
|
|
|
|
|
token: req.query.secret_token,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);*/
|
|
|
|
|
|
|
|
|
|
//export { authRouter };
|
|
|
|
|
|
|
|
|
|
export const AuthRouter = (appRouter: Express.Router) => {
|
|
|
|
|
const authRoutes: Express.Router = Express.Router({ mergeParams: true });
|
|
|
|
|
|
2024-05-16 11:56:46 +00:00
|
|
|
appRouter.use(registerMiddleware("isLoggedUser", isLoggedUser));
|
2024-05-15 19:56:22 +00:00
|
|
|
|
|
|
|
|
authRoutes.post(
|
|
|
|
|
"/login",
|
|
|
|
|
passport.authenticate("local-email", { session: false }),
|
|
|
|
|
(req: Express.Request, res: Express.Response, next: Express.NextFunction) =>
|
2024-05-16 11:56:46 +00:00
|
|
|
createLoginController(res.locals["context"]).execute(req, res, next),
|
2024-05-15 19:56:22 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
appRouter.use("/auth", authRoutes);
|
|
|
|
|
};
|