1+
2+
3+
4+ export async function approveHandler ( req , res ) {
5+ if ( req . method !== "POST" ) {
6+ return res . status ( 405 ) . end ( "Method Not Allowed" ) ;
7+ }
8+
9+ const { userId } = req . body ;
10+
11+ const response = await fetch ( `https://api.clerk.dev/v1/users/${ userId } ` , {
12+ method : "PATCH" ,
13+ headers : {
14+ Authorization : `Bearer ` + process . env . CLERK_SECRET_KEY ,
15+ "Content-Type" : "application/json" ,
16+ } ,
17+
18+ body : JSON . stringify ( {
19+ public_metadata : {
20+ // Set Clerk metadata to approved
21+ approved : true
22+ } ,
23+ } ) ,
24+ } ) ;
25+
26+ if ( ! response . ok ) {
27+ return res . status ( 500 ) . json ( { message : "Failed to approve user" } ) ;
28+ }
29+
30+ res . status ( 200 ) . json ( { message : "User approved" } ) ;
31+ }
32+
33+ export async function checkHandler ( req , res ) {
34+ const { userId } = req . body ;
35+
36+ if ( ! userId ) {
37+ return res . status ( 400 ) . json ( { error : "Missing userId" } ) ;
38+ }
39+
40+ const response = await fetch ( `https://api.clerk.dev/v1/users/${ userId } ` , {
41+ headers : {
42+ Authorization : `Bearer sk_test_wGcUTzeo4CJJ2iVUObnFRmkbaPT6pzAXcnonlv8q8w` , // GET RID OF HARDCODED EVENTUALLY
43+ } ,
44+ } ) ;
45+
46+ if ( ! response . ok ) {
47+ return res . status ( 500 ) . json ( { error : "Failed to fetch user from Clerk" } ) ;
48+ }
49+
50+ const user = await response . json ( ) ;
51+
52+ const approved = user . public_metadata ?. approved === true ;
53+ res . status ( 200 ) . json ( { approved } ) ;
54+ }
55+
56+ export async function unapprovedHandler ( req , res ) {
57+ console . log ( "Testing unapproved handler" ) ;
58+ try {
59+ const response = await fetch ( "https://api.clerk.dev/v1/users" , {
60+ headers : {
61+ Authorization : `Bearer ` + process . env . CLERK_SECRET_KEY ,
62+ } ,
63+ } ) ;
64+
65+ if ( ! response . ok ) {
66+ throw new Error ( `Clerk API error: ${ response . statusText } ` ) ;
67+ }
68+
69+ console . log ( "Response from Clerk API:" , response ) ;
70+
71+ const allUsers = await response . json ( ) ;
72+
73+ // Check the format of the response
74+ const usersArray = Array . isArray ( allUsers ) ? allUsers : allUsers ?. data || [ ] ;
75+
76+ const unapprovedUsers = usersArray
77+ . filter ( user => user . public_metadata ?. approved !== true )
78+ . map ( user => ( {
79+ id : user . id ,
80+ email : user . email_addresses ?. [ 0 ] ?. email_address || "Unknown" ,
81+ name : `${ user . first_name ?? "" } ${ user . last_name ?? "" } ` . trim ( )
82+ } ) ) ;
83+
84+ res . status ( 200 ) . json ( { users : unapprovedUsers } ) ;
85+ } catch ( error ) {
86+ console . error ( "Failed to fetch users:" , error ) ;
87+ res . status ( 500 ) . json ( { error : "Failed to fetch unapproved users" } ) ;
88+ }
89+ }
90+
91+ export default async function handler ( req , res ) {
92+ const { action } = req . query ;
93+
94+ console . log ( "Action:" , action ) ;
95+
96+ switch ( action ) {
97+ case 'approve' :
98+ return approveHandler ( req , res ) ;
99+ case 'check' :
100+ return checkHandler ( req , res ) ;
101+ case 'unapproved' :
102+ return unapprovedHandler ( req , res ) ;
103+ default :
104+ return res . status ( 400 ) . json ( { error : 'Invalid action' } ) ;
105+ }
106+ }
0 commit comments