(req: NextRequest)
| 97 | const headers = { "Content-Type": "application/json" }; |
| 98 | |
| 99 | export default async function handler(req: NextRequest) { |
| 100 | try { |
| 101 | console.log("/api/v1/confirm called!"); |
| 102 | // Handle CORS preflight request |
| 103 | if (req.method === "OPTIONS") { |
| 104 | return new Response(undefined, { status: 200 }); |
| 105 | } |
| 106 | // Handle non-POST requests |
| 107 | if (req.method !== "POST") { |
| 108 | return new Response( |
| 109 | JSON.stringify({ |
| 110 | error: "Only POST requests allowed", |
| 111 | }), |
| 112 | { |
| 113 | status: 405, |
| 114 | headers, |
| 115 | }, |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | // Authenticate that the user is allowed to use this API |
| 120 | let token = req.headers |
| 121 | .get("Authorization") |
| 122 | ?.replace("Bearer ", "") |
| 123 | .replace("bearer ", ""); |
| 124 | |
| 125 | if (!token) { |
| 126 | return new Response(JSON.stringify({ error: "Authentication failed" }), { |
| 127 | status: 401, |
| 128 | headers, |
| 129 | }); |
| 130 | } |
| 131 | |
| 132 | // Check that the user hasn't surpassed the rate limit |
| 133 | if (ratelimitProduction) { |
| 134 | const { success } = await ratelimitProduction.limit(token); |
| 135 | if (!success) { |
| 136 | return new Response( |
| 137 | JSON.stringify({ error: "Rate limit hit (30 requests/10s)" }), |
| 138 | { |
| 139 | status: 429, |
| 140 | headers, |
| 141 | }, |
| 142 | ); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | let org: OrgJoinIsPaid | null = null; |
| 147 | if (token) { |
| 148 | const authRes = await supabase |
| 149 | .from("organizations") |
| 150 | .select("*, is_paid(*)") |
| 151 | .eq("api_key", token) |
| 152 | .single(); |
| 153 | if (authRes.error) throw new Error(authRes.error.message); |
| 154 | org = authRes.data; |
| 155 | } |
| 156 | if (!org) { |
nothing calls this directly
no test coverage detected