Spaces:
Running
Running
Commit
·
5a6eb7f
1
Parent(s):
2cdb83a
refactor: more refactors
Browse files- .env.example +2 -3
- src/configs/config.ts +8 -0
- src/helpers/jwt.helper.ts +30 -19
- src/modules/console/index.route.ts +0 -11
- src/modules/console/users/controllers/users.controller.ts +30 -14
- src/modules/console/users/routes/admin-users.route.ts +0 -11
- src/modules/user/index.route.ts +0 -11
- src/modules/user/users/controllers/auth.controller.ts +1 -0
- src/modules/user/users/routes/auth.route.ts +0 -12
- src/routes.ts +0 -5
.env.example
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
PORT =
|
2 |
-
ACCESS_TOKEN_SECRET =
|
3 |
|
4 |
DB_URI =
|
5 |
|
6 |
-
|
7 |
-
|
|
|
1 |
PORT =
|
|
|
2 |
|
3 |
DB_URI =
|
4 |
|
5 |
+
JWT_SECRET =
|
6 |
+
JWT_EXPIRES_IN =
|
src/configs/config.ts
CHANGED
@@ -7,6 +7,10 @@ export interface Config {
|
|
7 |
db: {
|
8 |
uri: string;
|
9 |
};
|
|
|
|
|
|
|
|
|
10 |
}
|
11 |
|
12 |
export const config: Config = {
|
@@ -14,4 +18,8 @@ export const config: Config = {
|
|
14 |
db: {
|
15 |
uri: Env.get("DB_URI").toString(),
|
16 |
},
|
|
|
|
|
|
|
|
|
17 |
};
|
|
|
7 |
db: {
|
8 |
uri: string;
|
9 |
};
|
10 |
+
jwt: {
|
11 |
+
secret: string;
|
12 |
+
expiresIn: string;
|
13 |
+
};
|
14 |
}
|
15 |
|
16 |
export const config: Config = {
|
|
|
18 |
db: {
|
19 |
uri: Env.get("DB_URI").toString(),
|
20 |
},
|
21 |
+
jwt: {
|
22 |
+
secret: Env.get("JWT_SECRET").toString(),
|
23 |
+
expiresIn: Env.get("JWT_EXPIRES_IN").toString(),
|
24 |
+
},
|
25 |
};
|
src/helpers/jwt.helper.ts
CHANGED
@@ -1,23 +1,34 @@
|
|
1 |
import jwt from "jsonwebtoken";
|
|
|
2 |
|
3 |
export class jwtHelper {
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import jwt from "jsonwebtoken";
|
2 |
+
import { config } from "../configs/config";
|
3 |
|
4 |
export class jwtHelper {
|
5 |
+
static generateToken(payload: any) {
|
6 |
+
return jwt.sign(payload, config.jwt.secret, {
|
7 |
+
expiresIn: config.jwt.expiresIn,
|
8 |
+
});
|
9 |
+
}
|
10 |
|
11 |
+
static verifyToken(role: any) {
|
12 |
+
return (req: any, res: any, next: any) => {
|
13 |
+
let authHeader = req.headers["authorization"];
|
14 |
+
const token = authHeader && authHeader.split(" ")[1];
|
15 |
+
if (token) {
|
16 |
+
jwt.verify(token, config.jwt.secret, (err: any, tokenData: any) => {
|
17 |
+
if (err)
|
18 |
+
return res
|
19 |
+
.status(403)
|
20 |
+
.json({ success: false, code: 403, message: "Invalid Token!" });
|
21 |
+
if (!role.includes(tokenData.role))
|
22 |
+
return res
|
23 |
+
.status(401)
|
24 |
+
.json({ success: false, code: 401, message: "Unauthorized" });
|
25 |
+
req.tokenData = tokenData;
|
26 |
+
next();
|
27 |
+
});
|
28 |
+
} else
|
29 |
+
return res
|
30 |
+
.status(401)
|
31 |
+
.json({ success: false, code: 401, message: "Unauthorized" });
|
32 |
+
};
|
33 |
+
}
|
34 |
+
}
|
src/modules/console/index.route.ts
DELETED
@@ -1,11 +0,0 @@
|
|
1 |
-
import express from "express";
|
2 |
-
const adminRouter = express.Router();
|
3 |
-
|
4 |
-
import { jwtHelper } from "../../helpers/jwt.helper";
|
5 |
-
const allowedRoles = ["superAdmin", "admin"];
|
6 |
-
|
7 |
-
import { adminUserRoutes } from "./users/routes/admin-users.route";
|
8 |
-
|
9 |
-
adminRouter.use("/users", jwtHelper.verifyToken(allowedRoles), adminUserRoutes);
|
10 |
-
|
11 |
-
export { adminRouter };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/modules/console/users/controllers/users.controller.ts
CHANGED
@@ -1,18 +1,34 @@
|
|
|
|
|
|
|
|
|
|
1 |
import { usersService } from "../services/users.service";
|
|
|
2 |
|
3 |
-
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
}
|
18 |
-
}
|
|
|
|
1 |
+
import { jwtHelper } from "../../../../helpers/jwt.helper";
|
2 |
+
import { validator } from "../../../../helpers/validation.helper";
|
3 |
+
import { BaseController } from "../../../../lib/controllers/controller.base";
|
4 |
+
import { Prefix } from "../../../common/decorators/prefix.decorator";
|
5 |
import { usersService } from "../services/users.service";
|
6 |
+
import { adminUserValidation } from "../validation/admin.userValidation";
|
7 |
|
8 |
+
const allowedRoles = ["superAdmin", "admin"];
|
9 |
|
10 |
+
@Prefix("/admin/users")
|
11 |
+
export class adminUsersController extends BaseController {
|
12 |
+
static setRoutes(router) {
|
13 |
+
router.post(
|
14 |
+
"/create",
|
15 |
+
jwtHelper.verifyToken(allowedRoles),
|
16 |
+
validator(adminUserValidation.createValidation),
|
17 |
+
adminUsersController.create
|
18 |
+
);
|
19 |
+
}
|
20 |
+
|
21 |
+
static async create(req, res) {
|
22 |
+
try {
|
23 |
+
let result = await usersService.create(req.body);
|
24 |
+
return res.status(result.code).json(result);
|
25 |
+
} catch (err) {
|
26 |
+
console.log(`err.message`, err.message);
|
27 |
+
return res.status(500).json({
|
28 |
+
success: false,
|
29 |
+
code: 500,
|
30 |
+
error: err.message,
|
31 |
+
});
|
32 |
}
|
33 |
+
}
|
34 |
+
}
|
src/modules/console/users/routes/admin-users.route.ts
DELETED
@@ -1,11 +0,0 @@
|
|
1 |
-
import express from "express";
|
2 |
-
import { adminUsersController } from "../controllers/users.controller";
|
3 |
-
import { adminUserValidation } from "../validation/admin.userValidation";
|
4 |
-
import { validator } from "../../../../helpers/validation.helper";
|
5 |
-
|
6 |
-
const app = express.Router();
|
7 |
-
|
8 |
-
app.post("/create", validator(adminUserValidation.createValidation), adminUsersController.create);
|
9 |
-
|
10 |
-
export { app as adminUserRoutes };
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/modules/user/index.route.ts
DELETED
@@ -1,11 +0,0 @@
|
|
1 |
-
import express from 'express';
|
2 |
-
const app = express();
|
3 |
-
|
4 |
-
import { jwtHelper } from '../../helpers/jwt.helper';
|
5 |
-
const allowedRoles = ["user"];
|
6 |
-
|
7 |
-
import { authRoutes } from './users/routes/auth.route';
|
8 |
-
|
9 |
-
app.use(authRoutes);
|
10 |
-
|
11 |
-
export { app as userRoutes };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/modules/user/users/controllers/auth.controller.ts
CHANGED
@@ -5,6 +5,7 @@ import { validator } from "../../../../helpers/validation.helper";
|
|
5 |
import { userValidation } from "../validation/user.Validation";
|
6 |
import { Router } from "express";
|
7 |
import { Prefix } from "../../../common/decorators/prefix.decorator";
|
|
|
8 |
|
9 |
@Prefix("/user")
|
10 |
export class AuthController extends BaseController {
|
|
|
5 |
import { userValidation } from "../validation/user.Validation";
|
6 |
import { Router } from "express";
|
7 |
import { Prefix } from "../../../common/decorators/prefix.decorator";
|
8 |
+
import { Env } from "../../../../configs/env";
|
9 |
|
10 |
@Prefix("/user")
|
11 |
export class AuthController extends BaseController {
|
src/modules/user/users/routes/auth.route.ts
DELETED
@@ -1,12 +0,0 @@
|
|
1 |
-
import express from "express";
|
2 |
-
import { AuthController } from "../controllers/auth.controller";
|
3 |
-
import { userValidation } from "../validation/user.Validation";
|
4 |
-
import { validator } from "../../../../helpers/validation.helper";
|
5 |
-
|
6 |
-
const app = express.Router();
|
7 |
-
|
8 |
-
// app.post("/register", validator(userValidation.createValidation), AuthController.register);
|
9 |
-
// app.post("/login", validator(userValidation.loginValidation), AuthController.login);
|
10 |
-
|
11 |
-
export { app as authRoutes };
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/routes.ts
CHANGED
@@ -1,8 +1,5 @@
|
|
1 |
import { Router, Express } from "express";
|
2 |
|
3 |
-
import { userRoutes } from "./modules/user/index.route";
|
4 |
-
import { adminRouter } from "./modules/console/index.route";
|
5 |
-
|
6 |
import * as glob from "glob";
|
7 |
import path from "path";
|
8 |
import { BaseController } from "./lib/controllers/controller.base";
|
@@ -19,8 +16,6 @@ export const setAppRoutes = async (app: Express) => {
|
|
19 |
/* custom routes */
|
20 |
|
21 |
const setCustomRoutes = (router: Router) => {
|
22 |
-
router.use("/admin", adminRouter);
|
23 |
-
|
24 |
router.get("/health", (_req: any, res: any) => {
|
25 |
res
|
26 |
.status(200)
|
|
|
1 |
import { Router, Express } from "express";
|
2 |
|
|
|
|
|
|
|
3 |
import * as glob from "glob";
|
4 |
import path from "path";
|
5 |
import { BaseController } from "./lib/controllers/controller.base";
|
|
|
16 |
/* custom routes */
|
17 |
|
18 |
const setCustomRoutes = (router: Router) => {
|
|
|
|
|
19 |
router.get("/health", (_req: any, res: any) => {
|
20 |
res
|
21 |
.status(200)
|