From 8fa285cc4d7f72973396639d17683e223f2e7705 Mon Sep 17 00:00:00 2001 From: Louis Date: Sun, 22 Apr 2018 22:43:06 +0200 Subject: [PATCH] redirect user to last visited retreat page or home after slack sign in move constants to src/constants.js #8 --- helpers/airtable.js | 6 +++++- middleware/auth.js | 4 +--- middleware/charge.js | 2 +- routes/index.js | 10 ++++++---- src/constants.js | 22 ++++++++++++++++++++++ 5 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 src/constants.js diff --git a/helpers/airtable.js b/helpers/airtable.js index b141e5d..e6beebd 100644 --- a/helpers/airtable.js +++ b/helpers/airtable.js @@ -1,5 +1,9 @@ var Airtable = require('airtable') -var base = new Airtable({apiKey: process.env.AIRTABLE_API_KEY}).base(process.env.AIRTABLE_BASE_KEY) +const {airtableApiKey, airtableBaseKey} = require('../src/constants.js') + + +var base = new Airtable({apiKey: airtableApiKey}).base(airtableBaseKey) + module.exports = { retreat: base('Retreats'), diff --git a/middleware/auth.js b/middleware/auth.js index da78f2e..5b3fdac 100644 --- a/middleware/auth.js +++ b/middleware/auth.js @@ -1,8 +1,6 @@ const request = require('request') -const clientId = process.env.SLACK_CLIENT_ID -const clientSecret = process.env.SLACK_CLIENT_SECRET -const slackRedirectUri = process.env.SLACK_REDIRECT_URI const airtable = require('../helpers/airtable.js') +const {slackTeamName, slackClientId, slackRedirectUri, slackAuthorizeUri} = require('../src/constants.js') function checkCode(code) { diff --git a/middleware/charge.js b/middleware/charge.js index e8990dd..5aac0d6 100644 --- a/middleware/charge.js +++ b/middleware/charge.js @@ -1,4 +1,4 @@ -const stripeSecretKey = process.env.STRIPE_SECRET_KEY ? process.env.STRIPE_SECRET_KEY : 'sk_test_5T8O9RZssCDArs5Qvab937KI' +const {stripeSecretKey} = require('../src/constants.js') const stripe = require('stripe')(stripeSecretKey) const price = require('./price.js') diff --git a/routes/index.js b/routes/index.js index 7cafa7a..77c1240 100644 --- a/routes/index.js +++ b/routes/index.js @@ -10,6 +10,8 @@ const price = require('../middleware/price.js') const auth = require('../middleware/auth.js') const charge = require('../middleware/charge.js') +const {slackTeamName, slackClientId, slackRedirectUri, slackAuthorizeUri} = require('../src/constants.js') + router.get('/', function(req, res, next) { // find the "current" retreat and redirect to it @@ -22,7 +24,8 @@ router.get('/login', function (req, res, next) { // store last visited retreat req.session.oauth_return_uri = req.query.return_uri - // @todo redirect to slack + // redirect user to slack authorize + res.redirect(slackAuthorizeUri) }) router.get('/auth', function(req, res, next) { @@ -34,9 +37,8 @@ router.get('/auth', function(req, res, next) { .then(function(currentUserDetails) { // save user details into session req.session.currentUser = currentUserDetails - // redirect to home - res.redirect('/') - // @todo req.session.oauth_redirect_uri + // redirect to last visited retreat page or home + res.redirect('/' + (req.session.oauth_return_uri || '')) }, function (error) { next(error) }) diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 0000000..f719164 --- /dev/null +++ b/src/constants.js @@ -0,0 +1,22 @@ +require('dotenv').config() + +const slackTeamName = (process.env.SLACK_TEAM_NAME || 'mangroveteam') +const slackClientId = process.env.SLACK_CLIENT_ID +const slackRedirectUri = process.env.SLACK_REDIRECT_URI +const slackAuthorizeUri = 'https://slack.com/oauth/authorize?' + + 'team=' + slackTeamName + + '&scope=users:read&client_id=' + slackClientId + + '&redirect_uri=' + slackRedirectUri +const stripeSecretKey = process.env.STRIPE_SECRET_KEY ? process.env.STRIPE_SECRET_KEY : 'sk_test_5T8O9RZssCDArs5Qvab937KI' +const airtableApiKey = process.env.AIRTABLE_API_KEY +const airtableBaseKey = process.env.AIRTABLE_BASE_KEY + +module.exports = { + slackTeamName, + slackClientId, + slackRedirectUri, + slackAuthorizeUri, + stripeSecretKey, + airtableApiKey, + airtableBaseKey +}