@@ -5,7 +5,7 @@ import { createUser, deleteUser, updateUser } from "@/lib/actions/user.action";
5
5
import { NextResponse } from "next/server" ;
6
6
7
7
export async function POST ( req : Request ) {
8
- // You can find this in the Clerk Dashboard -> Webhooks -> choose the endpoint
8
+ // You can find this in the Clerk Dashboard -> Webhooks -> choose the webhook
9
9
const WEBHOOK_SECRET = process . env . NEXT_CLERK_WEBHOOK_SECRET ;
10
10
11
11
if ( ! WEBHOOK_SECRET ) {
@@ -31,7 +31,7 @@ export async function POST(req: Request) {
31
31
const payload = await req . json ( ) ;
32
32
const body = JSON . stringify ( payload ) ;
33
33
34
- // Create a new Svix instance with your secret.
34
+ // Create a new SVIX instance with your secret.
35
35
const wh = new Webhook ( WEBHOOK_SECRET ) ;
36
36
37
37
let evt : WebhookEvent ;
@@ -50,36 +50,29 @@ export async function POST(req: Request) {
50
50
} ) ;
51
51
}
52
52
53
- // Do something with the payload
54
- // For this guide, you simply log the payload to the console
55
53
const eventType = evt . type ;
56
54
57
55
if ( eventType === "user.created" ) {
58
56
const { id, email_addresses, image_url, username, first_name, last_name } =
59
57
evt . data ;
60
58
61
- // const mongoUser = await createUser({
62
- // clerkId: id,
63
- // name: `${first_name}${last_name ? ` ${last_name}` : ""}`,
64
- // username: username!,
65
- // email: email_addresses[0].email_address,
66
- // picture: image_url,
67
- // });
68
-
59
+ // Create a new user in your database
69
60
const mongoUser = await createUser ( {
70
61
clerkId : id ,
71
- name : `${ first_name } ${ last_name ? `${ last_name } ` : "" } ` ,
72
- username : username || "Userx" ,
62
+ name : `${ first_name } ${ last_name ? ` ${ last_name } ` : "" } ` ,
63
+ username : username ! ,
73
64
email : email_addresses [ 0 ] . email_address ,
74
65
picture : image_url ,
75
66
} ) ;
76
67
77
- return NextResponse . json ( { message : "Ok " , user : mongoUser } ) ;
68
+ return NextResponse . json ( { message : "OK " , user : mongoUser } ) ;
78
69
}
70
+
79
71
if ( eventType === "user.updated" ) {
80
72
const { id, email_addresses, image_url, username, first_name, last_name } =
81
73
evt . data ;
82
74
75
+ // Create a new user in your database
83
76
const mongoUser = await updateUser ( {
84
77
clerkId : id ,
85
78
updateData : {
@@ -91,7 +84,7 @@ export async function POST(req: Request) {
91
84
path : `/profile/${ id } ` ,
92
85
} ) ;
93
86
94
- return NextResponse . json ( { message : "Ok " , user : mongoUser } ) ;
87
+ return NextResponse . json ( { message : "OK " , user : mongoUser } ) ;
95
88
}
96
89
97
90
if ( eventType === "user.deleted" ) {
@@ -100,8 +93,9 @@ export async function POST(req: Request) {
100
93
const deletedUser = await deleteUser ( {
101
94
clerkId : id ! ,
102
95
} ) ;
103
- return NextResponse . json ( { message : "Ok" , user : deletedUser } ) ;
96
+
97
+ return NextResponse . json ( { message : "OK" , user : deletedUser } ) ;
104
98
}
105
99
106
- return new Response ( "" , { status : 200 } ) ;
100
+ return NextResponse . json ( { message : "OK" } ) ;
107
101
}
0 commit comments