Skip to content

Commit f998505

Browse files
committed
refactoring routers and middlewares
1 parent c58e0dc commit f998505

File tree

5 files changed

+35
-21
lines changed

5 files changed

+35
-21
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import passport from "passport";
22

3-
const JwtTokenValidador = passport.authenticate("jwt", { session: false });
3+
const getJwtTokenValidador = () => {
4+
return passport.authenticate("jwt", { session: false });
5+
};
46

5-
export { JwtTokenValidador };
7+
export { getJwtTokenValidador };

src/api/routers/authRouter.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { signIn, signUp, verifyAccount } from "../controllers/authControllers";
22
import { Router } from "express";
33

4-
const router = Router();
4+
const getAuthRouter = () => {
5+
const router = Router();
56

6-
router.post("/signup", signUp);
7+
router.post("/signup", signUp);
78

8-
router.post("/signin", signIn);
9+
router.post("/signin", signIn);
910

10-
router.get("/emailconfirmation/:token", verifyAccount);
11+
router.get("/emailconfirmation/:token", verifyAccount);
1112

12-
export { router as authRoutes };
13+
return router;
14+
};
15+
16+
export { getAuthRouter };

src/api/routers/homeRoutes.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { about } from "../../api/controllers/homeControllers";
22
import { Router } from "express";
33

4-
const router = Router();
4+
const getHomeRouter = () => {
5+
const router = Router();
56

6-
router.get("/about", about);
7+
router.get("/about", about);
78

8-
export { router as homeRoutes };
9+
return router;
10+
};
11+
12+
export { getHomeRouter };

src/api/routers/userRoutes.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { profile } from "../../api/controllers/userControllers";
22
import { Router } from "express";
3-
import { JwtTokenValidador } from "../middlewares/jwtTokenValidador";
3+
import { getJwtTokenValidador } from "../middlewares/jwtTokenValidador";
44

5-
const router = Router();
5+
const getUserRouter = () => {
6+
const router = Router();
67

7-
//As '/profile' is a protected route, we have to pass the JwtTokenValidador first as middleware before it arrives to the profile function.
8-
router.get("/profile", JwtTokenValidador, profile);
8+
//As '/profile' is a protected route, we have to pass the JwtTokenValidador first as middleware before it arrives to the profile function.
9+
router.get("/profile", getJwtTokenValidador(), profile);
910

10-
export { router as userRoutes };
11+
return router;
12+
};
13+
14+
export { getUserRouter };

src/config/appConfig.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import morgan from "morgan";
44
import helmet from "helmet";
55
import cookieParser from "cookie-parser";
66
import { configurePassportMiddlewares } from "./passportConfig";
7-
import { authRoutes } from "../api/routers/authRouter";
7+
import { getAuthRouter } from "../api/routers/authRouter";
88
import config from "./envConfig";
99
import cors from "cors";
10-
import { userRoutes } from "../api/routers/userRoutes";
11-
import { homeRoutes } from "../api/routers/homeRoutes";
10+
import { getUserRouter } from "../api/routers/userRoutes";
11+
import { getHomeRouter } from "../api/routers/homeRoutes";
1212

1313
const app: Application = express();
1414

@@ -33,9 +33,9 @@ configurePassportMiddlewares();
3333
//#endregion
3434

3535
//#region Routes
36-
app.use("/api/home/", homeRoutes);
37-
app.use("/api/auth", authRoutes);
38-
app.use("/api/user/", userRoutes);
36+
app.use("/api/home/", getHomeRouter());
37+
app.use("/api/auth", getAuthRouter());
38+
app.use("/api/user/", getUserRouter());
3939

4040
//#endregion
4141
export default app;

0 commit comments

Comments
 (0)