Skip to content

Commit

Permalink
Added missing user delete route.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Feb 4, 2025
1 parent 7d95202 commit fc4716a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/app/api/teams/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function POST(request: Request) {
name,
accessCode: `team_${getRandomChars(16)}`,
},
auth.user.userId,
auth.user.id,
);

return json(team);
Expand Down
31 changes: 28 additions & 3 deletions src/app/api/users/[userId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'zod';
import { canUpdateUser, canViewUser } from 'lib/auth';
import { getUser, getUserByUsername, updateUser } from 'queries';
import { json, unauthorized, badRequest } from 'lib/response';
import { canUpdateUser, canViewUser, canDeleteUser } from 'lib/auth';
import { getUser, getUserByUsername, updateUser, deleteUser } from 'queries';
import { json, unauthorized, badRequest, ok } from 'lib/response';
import { hashPassword } from 'next-basics';
import { parseRequest } from 'lib/request';

Expand Down Expand Up @@ -74,3 +74,28 @@ export async function POST(request: Request, { params }: { params: Promise<{ use

return json(updated);
}

export async function DELETE(
request: Request,
{ params }: { params: Promise<{ userId: string }> },
) {
const { auth, error } = await parseRequest(request);

if (error) {
return error();
}

const { userId } = await params;

if (!(await canDeleteUser(auth))) {
return unauthorized();
}

if (userId === auth.user.id) {
return badRequest('You cannot delete yourself.');
}

await deleteUser(userId);

return ok();
}
10 changes: 5 additions & 5 deletions src/app/api/websites/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function GET(request: Request) {
return error();
}

const websites = await getUserWebsites(auth.user.userId, query);
const websites = await getUserWebsites(auth.user.id, query);

return json(websites);
}
Expand All @@ -24,8 +24,8 @@ export async function POST(request: Request) {
const schema = z.object({
name: z.string().max(100),
domain: z.string().max(500),
shareId: z.string().max(50).nullable(),
teamId: z.string().nullable(),
shareId: z.string().max(50).nullable().optional(),
teamId: z.string().nullable().optional(),
});

const { auth, body, error } = await parseRequest(request, schema);
Expand All @@ -42,15 +42,15 @@ export async function POST(request: Request) {

const data: any = {
id: uuid(),
createdBy: auth.user.userId,
createdBy: auth.user.id,
name,
domain,
shareId,
teamId,
};

if (!teamId) {
data.userId = auth.user.userId;
data.userId = auth.user.id;
}

const website = await createWebsite(data);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export function unauthorized(message?: any) {
}

export function forbidden(message?: any) {
return Response.json({ error: 'Forbidden', message, status: 403 });
return Response.json({ error: 'Forbidden', message }, { status: 403 });
}

export function notFound(message?: any) {
return Response.json({ error: 'Not found', message, status: 404 });
return Response.json({ error: 'Not found', message }, { status: 404 });
}

export function serverError(error?: any) {
return Response.json({ error: 'Server error', message: serializeError(error), status: 500 });
return Response.json({ error: 'Server error', message: serializeError(error) }, { status: 500 });
}

0 comments on commit fc4716a

Please sign in to comment.