-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2392 from zeitgeistpm/onboarding-email
Verify user: email onboarding
- Loading branch information
Showing
4 changed files
with
108 additions
and
60 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
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 |
---|---|---|
@@ -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 }); | ||
} | ||
} |
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