From 926b59aaaa85f25fe091961584b3451ba91fd787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20R=C3=B6tsch?= Date: Fri, 29 Sep 2017 10:50:31 +0200 Subject: [PATCH] feat(credentials): allow setting of space id and tokens via query string --- app.js | 15 +++++++++++---- services/contentful.js | 18 +++++++++++++++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index e310a42..7145c28 100644 --- a/app.js +++ b/app.js @@ -37,6 +37,17 @@ app.use(function (req, res, next) { res.locals.query = req.query res.locals.currentPath = req.path console.log(req.path) + + // Allow setting of credentials via query parameters + const { space_id, preview_access_token, delivery_access_token } = req.query + if (space_id && preview_access_token && delivery_access_token) { // eslint-disable-line camelcase + const settings = {space: space_id, cda: delivery_access_token, cpa: preview_access_token} + res.cookie('universitySettings', settings, { maxAge: 900000, httpOnly: true }) + initClient(settings) + } else { + initClient(req.cookies.universitySettings) + } + next() }) @@ -65,8 +76,4 @@ app.use(function (err, req, res, next) { res.render('error') }) -// app.use() -// init the contentful client -initClient() - module.exports = app diff --git a/services/contentful.js b/services/contentful.js index b93bdb9..f6bd90d 100644 --- a/services/contentful.js +++ b/services/contentful.js @@ -3,9 +3,21 @@ const { createClient } = require('contentful') let cdaClient = null let cpaClient = null -exports.initClient = (config = {space: process.env.CF_SPACE, accessToken: process.env.CF_ACCESS_TOKEN}) => { - cdaClient = createClient(config) - cpaClient = createClient({space: process.env.CF_SPACE, accessToken: process.env.CF_PREVIEW_ACCESS_TOKEN, host: 'preview.contentful.com'}) +exports.initClient = (options) => { + const config = options || { + space: process.env.CF_SPACE, + cda: process.env.CF_ACCESS_TOKEN, + cpa: process.env.CF_PREVIEW_ACCESS_TOKEN + } + cdaClient = createClient({ + space: config.space, + accessToken: config.cda + }) + cpaClient = createClient({ + space: config.space, + accessToken: config.cpa, + host: 'preview.contentful.com' + }) } exports.getCourses = (locale = 'en-US', api = `cda`) => {