Skip to content

Commit

Permalink
Adding file next route validation (#100)
Browse files Browse the repository at this point in the history
* improve middleware next.js

* adding prettier rule in eslint config

---------

Co-authored-by: Marluan Espiritusanto <marluanespiritusanto@gmail.com>
  • Loading branch information
Victor1890 and marluanespiritusanto authored Oct 10, 2023
1 parent 9a90c65 commit 9e77ce4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@
"next/core-web-vitals",
"next",
"plugin:prettier/recommended"
]
],
"rules": {
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
]
}
}
8 changes: 8 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { NextResponse, type NextRequest } from 'next/server';

const isFileRegex = /(api|_next\/static|_next\/image|favicon\.ico|\/_next)/;

export async function middleware(request: NextRequest) {
const url = request.nextUrl.clone();
const response = NextResponse.next();

if (url.pathname.indexOf('.') != -1 || typeof window !== 'undefined')
return response;
if (isFileRegex.test(url.pathname)) return response;

const token = request.cookies.get('token');

if (!token || token.value !== process.env.SITE_COOKIE_KEY) {
Expand Down

0 comments on commit 9e77ce4

Please sign in to comment.