Skip to content

Commit

Permalink
Merge pull request #110 from Saurabhchaddha6/master
Browse files Browse the repository at this point in the history
protected routes
  • Loading branch information
dinxsh authored Sep 2, 2024
2 parents 3b0ac38 + 1548740 commit c64d405
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@ import { getToken } from 'next-auth/jwt';
export { default } from 'next-auth/middleware';

export const config = {
matcher: ['/dashboard/:path*', '/sign-in', '/sign-up', '/', '/verify/:path*'],
matcher: [
'/dashboard/:path*',
'/tournaments/:path*',
'/games/:path*',
'/teams/:path*',
'/blogs',
'/news',
'/contact',
'/sign-in',
'/sign-up',
'/',
'/verify/:path*'
],
};

export async function middleware(request: NextRequest) {
const token = await getToken({ req: request });
const url = request.nextUrl;


// If the user is signed in and tries to access sign-in, sign-up, verify, or the homepage, redirect them to the dashboard
if (
token &&
(url.pathname.startsWith('/sign-in') ||
Expand All @@ -21,7 +33,16 @@ export async function middleware(request: NextRequest) {
return NextResponse.redirect(new URL('/dashboard', request.url));
}

if (!token && url.pathname.startsWith('/dashboard')) {
// If the user is not signed in and tries to access protected routes, redirect them to the sign-in page
if (!token && (
url.pathname.startsWith('/dashboard') ||
url.pathname.startsWith('/tournaments') ||
url.pathname.startsWith('/games') ||
url.pathname.startsWith('/teams') ||
url.pathname.startsWith('/blogs') ||
url.pathname.startsWith('/news') ||
url.pathname.startsWith('/contact')
)) {
return NextResponse.redirect(new URL('/sign-in', request.url));
}

Expand Down

0 comments on commit c64d405

Please sign in to comment.