From 8f18892fac6d6a5f4a6a6c17d1a81e353e638bb9 Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Fri, 14 Jun 2019 19:23:59 -0300 Subject: [PATCH] feat: authentication callback --- app/routes/ecom/auth-callback.js | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/app/routes/ecom/auth-callback.js b/app/routes/ecom/auth-callback.js index ccacec3..5988c31 100644 --- a/app/routes/ecom/auth-callback.js +++ b/app/routes/ecom/auth-callback.js @@ -1 +1,34 @@ 'use strict' + +// log on files +const logger = require('console-files') + +module.exports = appSdk => { + return (req, res) => { + // E-Com Plus Store ID from request header + const storeId = parseInt(req.get('x-store-id'), 10) + + // handle callback with E-Com Plus app SDK + // https://github.com/ecomclub/ecomplus-app-sdk + appSdk.handleCallback(storeId, req.body) + + .then(({ isNew, authenticationId }) => { + // authentication tokens were updated + res.status(204) + res.end() + }) + + .catch(err => { + if (typeof err.code === 'string' && !err.code.startsWith('SQLITE_CONSTRAINT')) { + // debug SQLite errors + logger.error(err) + } + res.status(500) + let { message } = err + res.send({ + error: 'auth_callback_error', + message + }) + }) + } +}