Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RSN-74] Add middleware for handling protected routes #68

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Client/reasn-client/apps/web/lib/session.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
36 changes: 36 additions & 0 deletions Client/reasn-client/apps/web/middleware.ts
Original file line number Diff line number Diff line change
@@ -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/(.*)",
],
};
2 changes: 1 addition & 1 deletion Client/reasn-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]",
"engines": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import React from "react";
import { useRouter } from "next/navigation";

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
60 changes: 30 additions & 30 deletions Client/reasn-client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -16990,7 +16990,7 @@ __metadata:
optional: true
bin:
turbo: bin/turbo
checksum: 10c0/f02c06dfdb0339b91bea7d1c9f9f35f0a271df636e49f5f50ad444f6ae22d2205214a71ca31b197a665dc0bf995dc100e47d204c6e674550266efa910505eb7e
checksum: 10c0/5d4fbc5497fbc454e127ef3b8bd31bd556663e2db9fdc0e97f1282c4162e90d499a45809340fe9a5195f246c5c0d3aec57ea64b9d892f46f3f5a18a09619c7d9
languageName: node
linkType: hard

Expand Down
Loading