From 058ca859f2893c6deeb3e53ddaea2a469f2db516 Mon Sep 17 00:00:00 2001 From: raczu <Raczuekk@gmail.com> Date: Sun, 23 Jun 2024 14:29:14 +0200 Subject: [PATCH] feat: add routing middleware compliant with current routes --- Client/reasn-client/apps/web/lib/session.ts | 2 +- Client/reasn-client/apps/web/middleware.ts | 36 +++++++++++ Client/reasn-client/package.json | 2 +- .../ui/src/components/shared/Card.tsx | 2 + .../ui/src/components/shared/Comment.tsx | 5 +- Client/reasn-client/yarn.lock | 60 +++++++++---------- 6 files changed, 74 insertions(+), 33 deletions(-) create mode 100644 Client/reasn-client/apps/web/middleware.ts diff --git a/Client/reasn-client/apps/web/lib/session.ts b/Client/reasn-client/apps/web/lib/session.ts index ecdde0e4..d1bf8c64 100644 --- a/Client/reasn-client/apps/web/lib/session.ts +++ b/Client/reasn-client/apps/web/lib/session.ts @@ -1,5 +1,5 @@ import { jwtDecode, JwtPayload } from "jwt-decode"; -import { UserRole } from "@reasn/common/src/enums/modelsEnums"; +import { UserRole } from "@reasn/common/src/enums/schemasEnums"; import { getToken } from "@/lib/token"; export const SESSION_DEFAULT = { diff --git a/Client/reasn-client/apps/web/middleware.ts b/Client/reasn-client/apps/web/middleware.ts new file mode 100644 index 00000000..55effad4 --- /dev/null +++ b/Client/reasn-client/apps/web/middleware.ts @@ -0,0 +1,36 @@ +import type { NextRequest } from "next/server"; +import { NextResponse } from "next/server"; +import { getSession } from "@/lib/session"; +import { UserRole } from "@reasn/common/src/enums/schemasEnums"; + +export const middleware = (req: NextRequest) => { + const session = getSession(); + const path = req.nextUrl.pathname; + + const isAuthPath = path.startsWith("/login") || path.startsWith("/register"); + + if (!session.isAuthenticated()) { + if (isAuthPath) return NextResponse.next(); + return NextResponse.redirect(new URL("/login", req.url)); + } + + if (isAuthPath) { + return NextResponse.redirect(new URL("/me", req.url)); + } + + if (path.startsWith("/events") && session.user?.role === UserRole.USER) { + return NextResponse.redirect(new URL("/events", req.url)); + } +}; + +export const config = { + matcher: [ + "/events/new", + "/events/(.*)/(.*)", + "/me", + "/me/(.*)", + "/login", + "/register", + "/register/(.*)", + ], +}; diff --git a/Client/reasn-client/package.json b/Client/reasn-client/package.json index d1333d23..30f4d44b 100644 --- a/Client/reasn-client/package.json +++ b/Client/reasn-client/package.json @@ -27,7 +27,7 @@ "prettier-plugin-tailwindcss": "^0.6.1", "ts-jest": "^29.1.2", "ts-node": "^10.9.2", - "turbo": "latest" + "turbo": "^2.0.4" }, "packageManager": "yarn@4.1.0", "engines": { diff --git a/Client/reasn-client/packages/ui/src/components/shared/Card.tsx b/Client/reasn-client/packages/ui/src/components/shared/Card.tsx index 56b8ca47..73a92e01 100644 --- a/Client/reasn-client/packages/ui/src/components/shared/Card.tsx +++ b/Client/reasn-client/packages/ui/src/components/shared/Card.tsx @@ -1,3 +1,5 @@ +"use client"; + import React from "react"; import { useRouter } from "next/navigation"; diff --git a/Client/reasn-client/packages/ui/src/components/shared/Comment.tsx b/Client/reasn-client/packages/ui/src/components/shared/Comment.tsx index f10838a2..c2bceb92 100644 --- a/Client/reasn-client/packages/ui/src/components/shared/Comment.tsx +++ b/Client/reasn-client/packages/ui/src/components/shared/Comment.tsx @@ -1,5 +1,8 @@ import React from "react"; -import { CommentDto, CommentDtoMapper } from "@reasn/common/models/CommentDto"; +import { + CommentDto, + CommentDtoMapper, +} from "@reasn/common/src/schemas/CommentDto"; interface CommentProps { comment: CommentDto; diff --git a/Client/reasn-client/yarn.lock b/Client/reasn-client/yarn.lock index 1326e348..89634a10 100644 --- a/Client/reasn-client/yarn.lock +++ b/Client/reasn-client/yarn.lock @@ -14798,7 +14798,7 @@ __metadata: tailwindcss: "npm:^3.4.3" ts-jest: "npm:^29.1.2" ts-node: "npm:^10.9.2" - turbo: "npm:latest" + turbo: "npm:^2.0.4" zod: "npm:^3.23.7" languageName: unknown linkType: soft @@ -16923,58 +16923,58 @@ __metadata: languageName: node linkType: hard -"turbo-darwin-64@npm:1.13.2": - version: 1.13.2 - resolution: "turbo-darwin-64@npm:1.13.2" +"turbo-darwin-64@npm:2.0.4": + version: 2.0.4 + resolution: "turbo-darwin-64@npm:2.0.4" conditions: os=darwin & cpu=x64 languageName: node linkType: hard -"turbo-darwin-arm64@npm:1.13.2": - version: 1.13.2 - resolution: "turbo-darwin-arm64@npm:1.13.2" +"turbo-darwin-arm64@npm:2.0.4": + version: 2.0.4 + resolution: "turbo-darwin-arm64@npm:2.0.4" conditions: os=darwin & cpu=arm64 languageName: node linkType: hard -"turbo-linux-64@npm:1.13.2": - version: 1.13.2 - resolution: "turbo-linux-64@npm:1.13.2" +"turbo-linux-64@npm:2.0.4": + version: 2.0.4 + resolution: "turbo-linux-64@npm:2.0.4" conditions: os=linux & cpu=x64 languageName: node linkType: hard -"turbo-linux-arm64@npm:1.13.2": - version: 1.13.2 - resolution: "turbo-linux-arm64@npm:1.13.2" +"turbo-linux-arm64@npm:2.0.4": + version: 2.0.4 + resolution: "turbo-linux-arm64@npm:2.0.4" conditions: os=linux & cpu=arm64 languageName: node linkType: hard -"turbo-windows-64@npm:1.13.2": - version: 1.13.2 - resolution: "turbo-windows-64@npm:1.13.2" +"turbo-windows-64@npm:2.0.4": + version: 2.0.4 + resolution: "turbo-windows-64@npm:2.0.4" conditions: os=win32 & cpu=x64 languageName: node linkType: hard -"turbo-windows-arm64@npm:1.13.2": - version: 1.13.2 - resolution: "turbo-windows-arm64@npm:1.13.2" +"turbo-windows-arm64@npm:2.0.4": + version: 2.0.4 + resolution: "turbo-windows-arm64@npm:2.0.4" conditions: os=win32 & cpu=arm64 languageName: node linkType: hard -"turbo@npm:latest": - version: 1.13.2 - resolution: "turbo@npm:1.13.2" - dependencies: - turbo-darwin-64: "npm:1.13.2" - turbo-darwin-arm64: "npm:1.13.2" - turbo-linux-64: "npm:1.13.2" - turbo-linux-arm64: "npm:1.13.2" - turbo-windows-64: "npm:1.13.2" - turbo-windows-arm64: "npm:1.13.2" +"turbo@npm:^2.0.4": + version: 2.0.4 + resolution: "turbo@npm:2.0.4" + dependencies: + turbo-darwin-64: "npm:2.0.4" + turbo-darwin-arm64: "npm:2.0.4" + turbo-linux-64: "npm:2.0.4" + turbo-linux-arm64: "npm:2.0.4" + turbo-windows-64: "npm:2.0.4" + turbo-windows-arm64: "npm:2.0.4" dependenciesMeta: turbo-darwin-64: optional: true @@ -16990,7 +16990,7 @@ __metadata: optional: true bin: turbo: bin/turbo - checksum: 10c0/f02c06dfdb0339b91bea7d1c9f9f35f0a271df636e49f5f50ad444f6ae22d2205214a71ca31b197a665dc0bf995dc100e47d204c6e674550266efa910505eb7e + checksum: 10c0/5d4fbc5497fbc454e127ef3b8bd31bd556663e2db9fdc0e97f1282c4162e90d499a45809340fe9a5195f246c5c0d3aec57ea64b9d892f46f3f5a18a09619c7d9 languageName: node linkType: hard