Skip to content

Commit

Permalink
Use matcher to protect /api/invite
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinika committed Apr 11, 2024
1 parent 9fc4a86 commit 888ffdd
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/app/api/invite/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getServerSession } from 'next-auth/next';
import { NextRequest, NextResponse } from 'next/server';
import nextAuthMiddleware, { NextRequestWithAuth } from 'next-auth/middleware';
import { captureException } from '@sentry/nextjs';
import { Session } from 'next-auth';
import { authOptions } from '@/auth';
Expand All @@ -9,10 +8,7 @@ import { VlmError, isVlmError } from '@/types/virtual-lab/common';

export async function GET(req: NextRequest): Promise<any> {
const session = await getServerSession(authOptions);
if (!session) {
return nextAuthMiddleware(req as NextRequestWithAuth);
}
if (!session.accessToken) {
if (!session?.accessToken) {
return NextResponse.redirect(new URL(getErrorUrl(null, session, null), req.url));
}

Expand All @@ -21,7 +17,7 @@ export async function GET(req: NextRequest): Promise<any> {
return NextResponse.redirect(new URL(getErrorUrl(null, session, inviteToken ?? null), req.url));
}

const response = await processInvite(session.accessToken, inviteToken);
const response = await processInvite(session?.accessToken, inviteToken);
if (!isVlmInviteResponse(response)) {
const url = getErrorUrl(response, session, inviteToken);
return NextResponse.redirect(new URL(url, req.url));
Expand Down
1 change: 1 addition & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const config = {
matcher: [
'/',
'/main',
'/api/invite',
'/(build|simulate|simulations|main|explore|experiment-designer|svc|virtual-lab)/(.*)',
],
};

0 comments on commit 888ffdd

Please sign in to comment.