Spaces:
Running
Running
Merge pull request #28 from Modarb-Ai-Trainer:FixAndUserGuard
Browse files
package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
"description": "",
|
| 5 |
"main": "dist/index.js",
|
| 6 |
"scripts": {
|
| 7 |
-
"start": "tsc && tsc-alias && node dist/index.js",
|
| 8 |
"start:dev": "nodemon -r tsconfig-paths/register src/index.ts",
|
| 9 |
"build": "tsc",
|
| 10 |
"lint": "eslint . --ext .ts"
|
|
|
|
| 4 |
"description": "",
|
| 5 |
"main": "dist/index.js",
|
| 6 |
"scripts": {
|
| 7 |
+
"start": "rimraf dist && tsc && tsc-alias && node dist/index.js",
|
| 8 |
"start:dev": "nodemon -r tsconfig-paths/register src/index.ts",
|
| 9 |
"build": "tsc",
|
| 10 |
"lint": "eslint . --ext .ts"
|
src/modules/users/common/guards/users.guard.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { JwtPayload, verify } from "jsonwebtoken";
|
| 2 |
+
import { Request } from "express";
|
| 3 |
+
import { HttpError } from "@lib/error-handling/http-error";
|
| 4 |
+
import { config } from "@configs/config";
|
| 5 |
+
import { IJwtLoginPayload } from "@common/interfaces/jwt-payload.interface";
|
| 6 |
+
|
| 7 |
+
export const UsersGuardMiddleware =
|
| 8 |
+
() => (req: Request, res, next) => {
|
| 9 |
+
// get token from cookie
|
| 10 |
+
const token = req.headers.authorization?.split(" ")[1];
|
| 11 |
+
let payload: IJwtLoginPayload;
|
| 12 |
+
|
| 13 |
+
// validate token
|
| 14 |
+
if (!token) {
|
| 15 |
+
throw new HttpError(401, "Unauthorized");
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
try {
|
| 19 |
+
payload = verify(token, config.jwt.secret);
|
| 20 |
+
} catch (err) {
|
| 21 |
+
throw new HttpError(401, "Unauthorized");
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
if (payload.type !== "user") {
|
| 25 |
+
throw new HttpError(401, "Unauthorized");
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
// inject payload in request
|
| 30 |
+
(req as unknown as { jwtPayload: JwtPayload }).jwtPayload = payload;
|
| 31 |
+
|
| 32 |
+
// go on
|
| 33 |
+
next();
|
| 34 |
+
};
|
src/modules/users/exercises/controllers/exercises.controller.ts
CHANGED
|
@@ -8,9 +8,12 @@ import { BaseController } from "@lib/controllers/controller.base";
|
|
| 8 |
import { Prefix } from "@lib/decorators/prefix.decorator";
|
| 9 |
import { serialize } from "@helpers/serialize";
|
| 10 |
import { ExerciseSerialization } from "@common/serializers/exercise.serializtion";
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
@Prefix("/users/exercises")
|
|
|
|
| 14 |
export class ExerciseController extends BaseController {
|
| 15 |
private exercisesService = new ExerciseService();
|
| 16 |
|
|
|
|
| 8 |
import { Prefix } from "@lib/decorators/prefix.decorator";
|
| 9 |
import { serialize } from "@helpers/serialize";
|
| 10 |
import { ExerciseSerialization } from "@common/serializers/exercise.serializtion";
|
| 11 |
+
import { ControllerMiddleware } from "@lib/decorators/controller-middleware.decorator";
|
| 12 |
+
import { UsersGuardMiddleware } from "modules/users/common/guards/users.guard";
|
| 13 |
|
| 14 |
|
| 15 |
@Prefix("/users/exercises")
|
| 16 |
+
@ControllerMiddleware(UsersGuardMiddleware())
|
| 17 |
export class ExerciseController extends BaseController {
|
| 18 |
private exercisesService = new ExerciseService();
|
| 19 |
|
src/modules/users/workouts/controllers/workouts.controller.ts
CHANGED
|
@@ -8,8 +8,11 @@ import { BaseController } from "@lib/controllers/controller.base";
|
|
| 8 |
import { Prefix } from "@lib/decorators/prefix.decorator";
|
| 9 |
import { serialize } from "@helpers/serialize";
|
| 10 |
import { WorkoutSerialization } from "@common/serializers/workout.serializtion";
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@Prefix("/users/workouts")
|
|
|
|
| 13 |
export class WorkoutController extends BaseController {
|
| 14 |
private workoutsService = new WorkoutService();
|
| 15 |
|
|
|
|
| 8 |
import { Prefix } from "@lib/decorators/prefix.decorator";
|
| 9 |
import { serialize } from "@helpers/serialize";
|
| 10 |
import { WorkoutSerialization } from "@common/serializers/workout.serializtion";
|
| 11 |
+
import { ControllerMiddleware } from "@lib/decorators/controller-middleware.decorator";
|
| 12 |
+
import { UsersGuardMiddleware } from "modules/users/common/guards/users.guard";
|
| 13 |
|
| 14 |
@Prefix("/users/workouts")
|
| 15 |
+
@ControllerMiddleware(UsersGuardMiddleware())
|
| 16 |
export class WorkoutController extends BaseController {
|
| 17 |
private workoutsService = new WorkoutService();
|
| 18 |
|