feat(api): add account deletion endpoint with guest user protection
All checks were successful
Deploy Waveform to VPS / deploy (push) Successful in 7m31s
All checks were successful
Deploy Waveform to VPS / deploy (push) Successful in 7m31s
This commit is contained in:
parent
c48ff6543a
commit
fabd11e9e0
1 changed files with 41 additions and 1 deletions
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* @file app/api/users/profile/route.ts
|
||||
* @description API route handler for updating user profile settings (status, color).
|
||||
* @description API route handlers for updating user profile settings and deleting user accounts.
|
||||
*/
|
||||
|
||||
import { auth } from "@/auth";
|
||||
|
|
@ -10,6 +10,7 @@ import { MEMBER_COLOR_OPTIONS } from "@/lib/constants/member.styles";
|
|||
import { eq } from "drizzle-orm";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
/** List of valid user status values. */
|
||||
const VALID_STATUSES: UserStatus[] = ["ONLINE", "OFFLINE", "AFK", "DND"];
|
||||
|
||||
/** Handles PATCH requests to update the logged-in user's profile settings. */
|
||||
|
|
@ -75,3 +76,42 @@ export async function PATCH(req: Request) {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** Handles DELETE requests to remove the logged-in user's account. */
|
||||
export async function DELETE() {
|
||||
try {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const existingUser = await db.query.users.findFirst({
|
||||
where: eq(users.id, session.user.id),
|
||||
});
|
||||
|
||||
if (!existingUser) {
|
||||
return NextResponse.json({ error: "User not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Check if the email matches the GUEST_EMAIL from the .env file
|
||||
const guestEmail = process.env.GUEST_EMAIL;
|
||||
if (guestEmail && existingUser.email === guestEmail) {
|
||||
return NextResponse.json(
|
||||
{ error: "Guest accounts cannot be deleted" },
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
|
||||
// Perform deletion
|
||||
await db.delete(users).where(eq(users.id, session.user.id));
|
||||
|
||||
return NextResponse.json({ message: "Account deleted successfully" });
|
||||
} catch (error) {
|
||||
console.error("API User Settings DELETE error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal Server Error" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue