|
1 |
| -import { verifyMessage } from "viem"; |
2 |
| -import { createClient } from "@supabase/supabase-js"; |
3 |
| - |
4 |
| -const SUPABASE_KEY = process.env.SUPABASE_CLIENT_API_KEY; |
5 |
| -const SUPABASE_URL = process.env.SUPABASE_URL; |
6 |
| -const supabase = createClient(SUPABASE_URL!, SUPABASE_KEY!); |
7 |
| - |
8 |
| -export const uploadSettingsToSupabase = async function (event: any, context: any) { |
9 |
| - try { |
10 |
| - // Assuming the event.body contains the data we need |
11 |
| - const { email, signature, address } = event.body; |
12 |
| - console.log("🚀 ~ file: index.tsx:42 ~ handleSupabase ~ email:", email, signature, address); |
13 |
| - |
14 |
| - // Recover the address from the signature |
15 |
| - const recoveredAddress = await verifyMessage({ |
16 |
| - address, |
17 |
| - message: email, |
18 |
| - signature, |
19 |
| - }); |
20 |
| - console.log("🚀 ~ file: index.tsx:45 ~ handleSupabase ~ recoveredAddress:", recoveredAddress); |
21 |
| - |
22 |
| - // If the recovered address does not match the provided address, return an error |
23 |
| - if (!recoveredAddress) { |
24 |
| - throw new Error("Signature verification failed"); |
25 |
| - } |
26 |
| - |
27 |
| - // Allowed columns to update |
28 |
| - const allowedColumns = ["discord", "telegram", "twitter", "matrix", "push", "email"]; |
29 |
| - |
30 |
| - console.log("1"); |
31 |
| - |
32 |
| - // If the message is empty, delete the user record |
33 |
| - if (email === "{}") { |
34 |
| - let { data, error } = await supabase.from("users").delete().match({ address: recoveredAddress }); |
35 |
| - |
36 |
| - if (error) throw error; |
37 |
| - |
38 |
| - return { |
39 |
| - statusCode: 200, |
40 |
| - body: JSON.stringify({ message: "Record deleted successfully." }), |
41 |
| - }; |
42 |
| - } |
43 |
| - |
44 |
| - // Parse the signed message |
45 |
| - console.log("2", email); |
46 |
| - |
47 |
| - const parsedMessage = JSON.parse(JSON.stringify(email)); |
48 |
| - console.log("🚀 ~ file: index.tsx:69 ~ handleSupabase ~ parsedMessage:", parsedMessage); |
49 |
| - |
50 |
| - // Prepare the record data based on the allowed columns |
51 |
| - let recordData: { [key: string]: any } = {}; |
52 |
| - for (const key in parsedMessage) { |
53 |
| - if (allowedColumns.includes(key)) { |
54 |
| - recordData[key] = parsedMessage[key]; |
| 1 | +import { toast } from "react-toastify"; |
| 2 | +import { OPTIONS } from "utils/wrapWithToast"; |
| 3 | + |
| 4 | +export function uploadSettingsToSupabase(formData: any): Promise<Response> { |
| 5 | + console.log("form", formData); |
| 6 | + |
| 7 | + return toast.promise<Response, Error>( |
| 8 | + fetch("./netlify/functions/update-settings", { |
| 9 | + method: "POST", |
| 10 | + body: formData, |
| 11 | + }).then(async (response) => { |
| 12 | + if (response.status !== 200) { |
| 13 | + const error = await response.json().catch(() => ({ message: "Error uploading to Supabase" })); |
| 14 | + throw new Error(error.message); |
55 | 15 | }
|
56 |
| - } |
57 |
| - |
58 |
| - // Assuming you have a 'users' table with 'address' and allowedColumns fields |
59 |
| - let { data, error } = await supabase |
60 |
| - .from("user-settings") |
61 |
| - .upsert({ address, email: email }) |
62 |
| - .match({ address: address }); |
63 |
| - |
64 |
| - console.log("🚀 ~ file: index.tsx:89 ~ handleSupabase ~ data:", data); |
65 |
| - if (error) throw error; |
66 |
| - |
67 |
| - return { |
68 |
| - statusCode: 200, |
69 |
| - body: JSON.stringify({ message: "Record updated successfully." }), |
70 |
| - }; |
71 |
| - } catch (err) { |
72 |
| - return { |
73 |
| - statusCode: 500, |
74 |
| - body: JSON.stringify({ message: `Error: ` }), |
75 |
| - }; |
76 |
| - } |
77 |
| -}; |
| 16 | + return response; |
| 17 | + }), |
| 18 | + { |
| 19 | + pending: "Uploading settings to Supabase...", |
| 20 | + success: "Uploaded successfully!", |
| 21 | + error: { |
| 22 | + render({ data: error }) { |
| 23 | + return `Upload failed: ${error?.message}`; |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + OPTIONS |
| 28 | + ); |
| 29 | +} |
0 commit comments