-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6c2582
commit 6f9a5db
Showing
5 changed files
with
347 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
web/src/layout/Header/navbar/Menu/Settings/SendMeNotifications/FormNotifs/FormEmail.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { verifyMessage } from "viem"; | ||
import { createClient } from "@supabase/supabase-js"; | ||
|
||
const SUPABASE_KEY = process.env.SUPABASE_CLIENT_API_KEY; | ||
const SUPABASE_URL = process.env.SUPABASE_URL; | ||
const supabase = createClient(SUPABASE_URL!, SUPABASE_KEY!); | ||
|
||
export const uploadSettingsToSupabase = async function (event: any, context: any) { | ||
try { | ||
// Assuming the event.body contains the data we need | ||
const { email, signature, address } = event.body; | ||
console.log("π ~ file: index.tsx:42 ~ handleSupabase ~ email:", email, signature, address); | ||
|
||
// Recover the address from the signature | ||
const recoveredAddress = await verifyMessage({ | ||
address, | ||
message: email, | ||
signature, | ||
}); | ||
console.log("π ~ file: index.tsx:45 ~ handleSupabase ~ recoveredAddress:", recoveredAddress); | ||
|
||
// If the recovered address does not match the provided address, return an error | ||
if (!recoveredAddress) { | ||
throw new Error("Signature verification failed"); | ||
} | ||
|
||
// Allowed columns to update | ||
const allowedColumns = ["discord", "telegram", "twitter", "matrix", "push", "email"]; | ||
|
||
console.log("1"); | ||
|
||
// If the message is empty, delete the user record | ||
if (email === "{}") { | ||
let { data, error } = await supabase.from("users").delete().match({ address: recoveredAddress }); | ||
|
||
if (error) throw error; | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ message: "Record deleted successfully." }), | ||
}; | ||
} | ||
|
||
// Parse the signed message | ||
console.log("2", email); | ||
|
||
const parsedMessage = JSON.parse(JSON.stringify(email)); | ||
console.log("π ~ file: index.tsx:69 ~ handleSupabase ~ parsedMessage:", parsedMessage); | ||
|
||
// Prepare the record data based on the allowed columns | ||
let recordData: { [key: string]: any } = {}; | ||
for (const key in parsedMessage) { | ||
if (allowedColumns.includes(key)) { | ||
recordData[key] = parsedMessage[key]; | ||
} | ||
} | ||
|
||
// Assuming you have a 'users' table with 'address' and allowedColumns fields | ||
let { data, error } = await supabase | ||
.from("user-settings") | ||
.upsert({ address, email: email }) | ||
.match({ address: address }); | ||
|
||
console.log("π ~ file: index.tsx:89 ~ handleSupabase ~ data:", data); | ||
if (error) throw error; | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ message: "Record updated successfully." }), | ||
}; | ||
} catch (err) { | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ message: `Error: ` }), | ||
}; | ||
} | ||
}; |
Oops, something went wrong.