Skip to content

Commit

Permalink
refacto: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
quentingrchr committed Oct 3, 2024
1 parent 4c7d300 commit 2ad0df8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/app/api/teams/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import db from '@/lib/db';
import { AuthApiRequest } from '@/lib/router';
import { checkAuthAppRouter } from '@/lib/middleware';
import { HandlerApiError, HandlerApiResponse } from '@/utils/handlerResponse';
import { Team } from '@prisma/client';
import { NextApiResponse } from 'next';
import { getServerSession } from 'next-auth';
import { createRouter } from 'next-connect';
import { NextRequest } from 'next/server';
import { createEdgeRouter } from 'next-connect';
import type { NextRequest } from 'next/server';
import urlSlug from 'url-slug';
import options from '../auth/[...nextauth]/options';

export type ApiTeamResponseSuccess = Team;

export const router = createRouter<AuthApiRequest, NextApiResponse>();
const router = createEdgeRouter<NextRequest, {}>();

const RESTRICTED_TEAM_NAMES = [
'create',
Expand All @@ -22,10 +21,9 @@ const RESTRICTED_TEAM_NAMES = [
'tags',
];

export async function POST(req: NextRequest) {
router.use(checkAuthAppRouter).post(async (req, event, next) => {
try {
const session = await getServerSession(options);

if (!session) return HandlerApiError.unauthorized();
const { teamName } = await req.json();
if (!teamName) return HandlerApiError.badRequest();
Expand Down Expand Up @@ -87,4 +85,8 @@ export async function POST(req: NextRequest) {
} catch (error) {
return HandlerApiError.internalServerError();
}
}
});

export async function POST(request: NextRequest, ctx: {}) {
return router.run(request, ctx);
}
19 changes: 18 additions & 1 deletion src/lib/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { default as authOptions } from '@/app/api/auth/[...nextauth]/options';
import options, {
default as authOptions,
} from '@/app/api/auth/[...nextauth]/options';
import { checkDigestAuth } from '@/services/database/digest';
import { getTeamMembershipById } from '@/services/database/membership';
import { getTeamById } from '@/services/database/team';
import { HandlerApiError } from '@/utils/handlerResponse';
import { NextApiResponse } from 'next';
import { getServerSession } from 'next-auth';
import { NextHandler } from 'next-connect';
import { NextRequest } from 'next/server';
import { AuthApiRequest } from './router';

export const checkProAccount = async (
Expand Down Expand Up @@ -66,6 +70,19 @@ export const checkAuth = async (
return next();
};

export const checkAuthAppRouter = async (
req: NextRequest,
event: {},
next: NextHandler
) => {
const session = await getServerSession(options);

if (!session) return HandlerApiError.unauthorized();
const { teamName } = await req.json();
if (!teamName) return HandlerApiError.badRequest();

return next();
};

export const checkDigest = async (
req: AuthApiRequest,
Expand Down

0 comments on commit 2ad0df8

Please sign in to comment.