Webhook updates
This commit is contained in:
parent
494d18c3e3
commit
692685b1e7
2 changed files with 59 additions and 2 deletions
55
src/pages/api/webhooks/onUserCreate.ts
Normal file
55
src/pages/api/webhooks/onUserCreate.ts
Normal file
|
@ -0,0 +1,55 @@
|
|||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { env } from "~/env.mjs";
|
||||
import { validateRequest } from "~/server/unkey";
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
const success = await validateRequest(req, res);
|
||||
|
||||
if (success) {
|
||||
const requestBody = req.body as {
|
||||
data: {
|
||||
id: string;
|
||||
email_addresses: [
|
||||
{
|
||||
email_address: string;
|
||||
id: string;
|
||||
verification: {
|
||||
status: string;
|
||||
strategy: string;
|
||||
};
|
||||
}
|
||||
];
|
||||
};
|
||||
object: string;
|
||||
type: string;
|
||||
};
|
||||
|
||||
const userUpdateResponse = await fetch(
|
||||
`https://api.clerk.com/v1/users/${requestBody.data.id}/metadata`,
|
||||
{
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
Authorization: `Bearer ${env.CLERK_SECRET_KEY}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
public_metadata: {
|
||||
isVIP: false,
|
||||
isAdmin: false,
|
||||
},
|
||||
private_metadata: {},
|
||||
unsafe_metadata: {},
|
||||
}),
|
||||
}
|
||||
);
|
||||
|
||||
if (userUpdateResponse.status === 200) {
|
||||
res.status(200).json({ result: "METADATA UPDATED" });
|
||||
} else {
|
||||
res.status(500).json({ error: "ERROR UPDATING METADATA" });
|
||||
}
|
||||
}
|
||||
}
|
|
@ -28,8 +28,10 @@ export default async function handler(
|
|||
if (deletedRoom.rowsAffected > 0) {
|
||||
await db.delete(logs).where(eq(logs.userId, requestBody.data.id));
|
||||
await db.delete(votes).where(eq(votes.userId, requestBody.data.id));
|
||||
}
|
||||
|
||||
res.status(200).json({ result: "USER DELETED" });
|
||||
res.status(200).json({ result: "USER DELETED" });
|
||||
} else {
|
||||
res.status(500).json({ result: "ERROR DELETING USER" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue