From 3c49a9c3e1c1e88752862e5f94e90109be517fe3 Mon Sep 17 00:00:00 2001 From: Antariksh Date: Wed, 16 Jun 2021 19:05:15 +0800 Subject: [PATCH] docs: collapse code examples into one --- README.md | 47 ++++++++--------------------------------------- 1 file changed, 8 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index bcdf450..cdaa3ca 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,8 @@ app.post( // Parse JSON from raw request body express.json(), // Decrypt the submission - function (req, res, next) { + async function (req, res, next) { + // Use `decrypt` if the submission does not contain attachments const submission = formsg.crypto.decrypt( formSecretKey, // If `verifiedContent` is provided in `req.body.data`, the return object @@ -70,46 +71,14 @@ app.post( req.body.data ) - // If the decryption failed, submission will be `null`. - if (submission) { - // Continue processing the submission - } else { - // Could not decrypt the submission - } - } -) + // Use `decryptWithAttachments` if the submission contains attachments. The result + // will contain the decrypted submission and attachments. + const submissionWithAttachments = + await formsg.crypto.decryptWithAttachments(formSecretKey, req.body.data) -// Example for submissions with attachments -app.post( - '/submissions-attachment', - // Endpoint authentication by verifying signatures - function (req, res, next) { - try { - formsg.webhooks.authenticate(req.get('X-FormSG-Signature'), POST_URI) - // Continue processing the POST body - return next() - } catch (e) { - return res.status(401).send({ message: 'Unauthorized' }) - } - }, - // Parse JSON from raw request body - express.json(), - // Decrypt the submission and attachments - async function (req, res, next) { - // Note that this function is only available from version 0.9.0 onwards - const submission = await formsg.crypto.decryptWithAttachments( - formSecretKey, - // If `verifiedContent` is provided in `req.body.data`, the return object - // will include a verified key. - req.body.data - ) - - // If the decryption failed at any point, submission will be `null`. - if (submission) { + // If the decryption failed, submission or submissionWithAttachments will be `null`. + if (submission) { // or if (submissionWithAttachments) // Continue processing the submission - - // processSubmission(submission.content) - // processAttachments(submission.attachments) } else { // Could not decrypt the submission }