Skip to content

Commit 0927267

Browse files
committed
refactor: netlify function
1 parent 6f9a5db commit 0927267

File tree

3 files changed

+97
-83
lines changed

3 files changed

+97
-83
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { createClient } from "@supabase/supabase-js";
2+
import { ethers } from "ethers";
3+
4+
// Configuration
5+
const SUPABASE_KEY = process.env.SUPABASE_CLIENT_API_KEY;
6+
const SUPABASE_URL = process.env.SUPABASE_URL;
7+
const supabase = createClient(SUPABASE_URL!, SUPABASE_KEY!);
8+
9+
export const handler = async function (event: any, context: any) {
10+
try {
11+
// Assuming the event.body contains the data we need
12+
const { message, signature, address } = JSON.parse(event.body);
13+
14+
// Recover the address from the signature
15+
const recoveredAddress = ethers.utils.verifyMessage(message, signature);
16+
17+
// If the recovered address does not match the provided address, return an error
18+
if (recoveredAddress !== address) {
19+
throw new Error("Signature verification failed");
20+
}
21+
22+
// Allowed columns to update
23+
const allowedColumns = ["discord", "telegram", "twitter", "matrix", "push", "email"];
24+
25+
// If the message is empty, delete the user record
26+
if (message === "{}") {
27+
let { data, error } = await supabase.from("users").delete().match({ address: recoveredAddress });
28+
29+
if (error) throw error;
30+
31+
return {
32+
statusCode: 200,
33+
body: JSON.stringify({ message: "Record deleted successfully." }),
34+
};
35+
}
36+
37+
// Parse the signed message
38+
const parsedMessage = JSON.parse(message);
39+
40+
// Prepare the record data based on the allowed columns
41+
let recordData: { [key: string]: any } = {};
42+
for (const key in parsedMessage) {
43+
if (allowedColumns.includes(key)) {
44+
recordData[key] = parsedMessage[key];
45+
}
46+
}
47+
48+
// Assuming you have a 'users' table with 'address' and allowedColumns fields
49+
let { data, error } = await supabase.from("user-settings").upsert(recordData).match({ address: recoveredAddress });
50+
51+
if (error) throw error;
52+
53+
return {
54+
statusCode: 200,
55+
body: JSON.stringify({ message: "Record updated successfully." }),
56+
};
57+
} catch (err) {
58+
return {
59+
statusCode: 500,
60+
body: JSON.stringify({ message: `Error: ` }),
61+
};
62+
}
63+
};

web/src/layout/Header/navbar/Menu/Settings/SendMeNotifications/FormNotifs/index.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,15 @@ const FormNotifs: React.FC = () => {
5050
account: address,
5151
message: emailInput,
5252
});
53-
const data = {
54-
body: {
55-
email: emailInput,
53+
const data = [
54+
{
55+
message: emailInput,
5656
address,
5757
signature: tx,
5858
},
59-
};
60-
toast.info("Updating notification settings", toastOptions);
61-
await uploadSettingsToSupabase(data, {});
62-
toast.success("Update is successful", toastOptions);
59+
];
60+
61+
await uploadSettingsToSupabase(data);
6362
console.log("🚀 ~ file: index.tsx:123 ~ handleSubmit ~ tx:", tx);
6463
};
6564

Lines changed: 28 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,29 @@
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);
5515
}
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

Comments
 (0)