Skip to content

Commit

Permalink
Merge pull request #2392 from zeitgeistpm/onboarding-email
Browse files Browse the repository at this point in the history
Verify user: email onboarding
  • Loading branch information
robhyrk authored May 15, 2024
2 parents fa01b9f + b7aa4ee commit 92927ad
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 60 deletions.
43 changes: 30 additions & 13 deletions lib/hooks/useWeb3Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ const useWeb3Wallet = () => {
}
};

const onboardUser = async () => {
if (!web3auth) {
return;
}
const user = await web3auth.getUserInfo();
if (!user?.idToken) {
return;
}
const base64Url = user?.idToken.split(".")[1];
const base64 = base64Url.replace("-", "+").replace("_", "/");
const parsedToken = JSON.parse(window.atob(base64));
try {
const res = await fetch("/api/onboardUser", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + user.idToken,
},
body: JSON.stringify({
email: user.email,
name: user.name,
appPubKey: parsedToken.wallets[0].public_key,
}),
});
} catch (e) {
return;
}
};

const login = async (loginOptions: loginOptions) => {
if (!web3auth || !auth0Domain) {
notificationStore.pushNotification(
Expand All @@ -64,19 +93,7 @@ const useWeb3Wallet = () => {
);
if (web3authProvider) {
await getKeypair(web3authProvider);
// TODO: refactor user onboarding
// const user = await web3auth.getUserInfo();
// try {
// const res = await fetch("/api/onboardUser", {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({ email: user.email, name: user.name }),
// });
// } catch (e) {
// return;
// }
await onboardUser();
}
} catch (e) {
notificationStore.pushNotification(
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"autoprefixer": "10.2.5",
"commander": "^8.3.0",
"cross-env": "^7.0.3",
"jose": "^5.2.4",
"moment": "^2.29.1",
"next-transpile-modules": "^9.1.0",
"postcss": "8.2.13",
Expand Down
116 changes: 69 additions & 47 deletions pages/api/onboardUser.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,74 @@
// import { NextApiRequest, NextApiResponse } from "next";
// const SibApiV3Sdk = require("@getbrevo/brevo");
import { NextApiRequest, NextApiResponse } from "next";
import * as jose from "jose";

// export default async function onboardUser(
// request: NextApiRequest,
// response: NextApiResponse,
// ) {
// //change this value to match corresponding list id in Brevo
// const listId = 37;
// const userEmail = request.body.email;
// const userName = request.body.name;
const SibApiV3Sdk = require("@getbrevo/brevo");

// const apiInstance = new SibApiV3Sdk.ContactsApi();
// const apiKey = apiInstance.authentications["apiKey"];
// apiKey.apiKey = process.env.BREVO_API;
export default async function onboardUser(
req: NextApiRequest,
res: NextApiResponse,
) {
//verify user auth
try {
const idToken = req.headers.authorization?.split(" ")[1] || "";
const app_pub_key = req.body.appPubKey;
const jwks = jose.createRemoteJWKSet(
new URL("https://api.openlogin.com/jwks"),
);
const jwtDecoded = await jose.jwtVerify(idToken, jwks, {
algorithms: ["ES256"],
});

// const createContact = new SibApiV3Sdk.CreateContact();
// createContact.attributes = { NAME: userName };
// createContact.email = userEmail;
// createContact.listIds = [listId];
if ((jwtDecoded.payload as any).wallets[0].public_key == app_pub_key) {
// verified
const listId = 37; //change this value to match corresponding list id in Brevo
const userEmail = req.body.email;
const userName = req.body.name;

// const contactEmails = new SibApiV3Sdk.AddContactToList();
// contactEmails.emails = [request.body.email];
const apiInstance = new SibApiV3Sdk.ContactsApi();
const apiKey = apiInstance.authentications["apiKey"];
apiKey.apiKey = process.env.BREVO_API;

// try {
// const contactInfo = await apiInstance.getContactInfo(userEmail);
// if (contactInfo.body.email) {
// try {
// await apiInstance.addContactToList(listId, contactEmails);
// return response
// .status(200)
// .json({ success: true, message: "Contact added to list" });
// } catch (error) {
// return response
// .status(400)
// .json({ success: false, message: "Contact already in list" });
// }
// }
// } catch (error) {
// try {
// await apiInstance.createContact(createContact);
// return response.status(200).json({
// success: true,
// message: "Contact created and added to list",
// });
// } catch (error) {
// return response
// .status(400)
// .json({ success: false, message: "Error creating contact" });
// }
// }
// }
const createContact = new SibApiV3Sdk.CreateContact();
createContact.attributes = { NAME: userName };
createContact.email = userEmail;
createContact.listIds = [listId];

const contactEmails = new SibApiV3Sdk.AddContactToList();
contactEmails.emails = [req.body.email];

try {
const contactInfo = await apiInstance.getContactInfo(userEmail);
if (contactInfo.body.email) {
try {
await apiInstance.addContactToList(listId, contactEmails);
return res
.status(200)
.json({ success: true, message: "Contact added to list" });
} catch (error) {
return res
.status(400)
.json({ success: false, message: "Contact already in list" });
}
}
} catch (error) {
try {
await apiInstance.createContact(createContact);
return res.status(200).json({
success: true,
message: "Contact created and added to list",
});
} catch (error) {
return res
.status(400)
.json({ success: false, message: "Error creating contact" });
}
}
} else {
// verification failed
res.status(401).json({ name: "Validation Failed" });
console.log("Validation Failed");
}
} catch (error) {
res.status(500).json({ error: error.message });
}
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5078,6 +5078,7 @@ __metadata:
groq: ^3.23.4
ipfs-http-client: ^60.0.1
ipfs-utils: ^9.0.14
jose: ^5.2.4
jotai: ^2.0.4
jotai-tanstack-query: ^0.7.0
lodash.merge: ^4.6.2
Expand Down Expand Up @@ -9829,6 +9830,13 @@ __metadata:
languageName: node
linkType: hard

"jose@npm:^5.2.4":
version: 5.2.4
resolution: "jose@npm:5.2.4"
checksum: 81e1f4494f406debd14392975327f0daa8b88ff09c83f2fe94754dcd7cfdefdb1adf785b2ec7481751927e609d342581cd41cb444628ef62fca423facebcd280
languageName: node
linkType: hard

"jotai-tanstack-query@npm:^0.7.0":
version: 0.7.2
resolution: "jotai-tanstack-query@npm:0.7.2"
Expand Down

0 comments on commit 92927ad

Please sign in to comment.