diff --git a/graph/context.js b/graph/context.js index 308c86f41..adf5be06a 100644 --- a/graph/context.js +++ b/graph/context.js @@ -46,16 +46,16 @@ const decorateContextPlugins = (context, contextPlugins) => { * Stores the request context. */ class Context { - constructor(parent) { + constructor(ctx) { // Generate a new context id for the request if the parent doesn't provide // one. - this.id = parent.id || uuid.v4(); + this.id = ctx.id || uuid.v4(); // Attach a logger or create one. - this.log = parent.log || createLogger('context', this.id); + this.log = ctx.log || createLogger('context', this.id); // Load the current logged in user to `user`, otherwise this will be null. - this.user = get(parent, 'user'); + this.user = get(ctx, 'user'); // Attach the connectors. this.connectors = connectors; @@ -73,7 +73,7 @@ class Context { this.pubsub = getBroker(); // Bind the parent context. - this.parent = parent; + this.parent = ctx; } /** diff --git a/middleware/authentication.js b/middleware/authentication.js index 5f6009136..b9663e768 100644 --- a/middleware/authentication.js +++ b/middleware/authentication.js @@ -1,4 +1,5 @@ const { passport } = require('../services/passport'); +const Context = require('../graph/context'); const debug = require('debug')('talk:middleware:authentication'); const authentication = (req, res, next) => @@ -22,6 +23,9 @@ const authentication = (req, res, next) => debug('user was not on request'); } + // Attach a new context to the request. + req.context = new Context(req); + next(); } )(req, res, next); diff --git a/plugins/talk-plugin-facebook-auth/server/router.js b/plugins/talk-plugin-facebook-auth/server/router.js index fd6253fbe..7fffc0139 100644 --- a/plugins/talk-plugin-facebook-auth/server/router.js +++ b/plugins/talk-plugin-facebook-auth/server/router.js @@ -4,9 +4,12 @@ module.exports = router => { * for authorization. */ router.get('/api/v1/auth/facebook', (req, res, next) => { - const { connectors: { services: { Passport } } } = req.context; + const { + connectors: { services: { Passport: { passport } } }, + } = req.context; + console.log(req.context.connectors.services); - return Passport.authenticate('facebook', { + return passport.authenticate('facebook', { display: 'popup', authType: 'rerequest', scope: ['public_profile'], @@ -18,11 +21,14 @@ module.exports = router => { * send back the user credentials upon successful login. */ router.get('/api/v1/auth/facebook/callback', (req, res, next) => { - const { connectors: { services: { Passport } } } = req.context; - const { HandleAuthPopupCallback } = Passport; + const { + connectors: { + services: { Passport: { passport, HandleAuthPopupCallback } }, + }, + } = req.context; // Perform the facebook login flow and pass the data back through the opener. - Passport.authenticate( + passport.authenticate( 'facebook', { session: false }, HandleAuthPopupCallback(req, res, next) diff --git a/routes/index.js b/routes/index.js index 926d5e2b5..88c615281 100644 --- a/routes/index.js +++ b/routes/index.js @@ -11,7 +11,6 @@ const plugins = require('../services/plugins'); const staticTemplate = require('../middleware/staticTemplate'); const staticMiddleware = require('express-static-gzip'); const { DISABLE_STATIC_SERVER } = require('../config'); -const Context = require('../graph/context'); const { passport } = require('../services/passport'); const { MOUNT_PATH } = require('../url'); @@ -66,12 +65,6 @@ if (!DISABLE_STATIC_SERVER) { // Add the i18n middleware to all routes. router.use(i18n); -// Bind a new context to the request. -router.use((req, res, next) => { - req.context = new Context(req); - next(); -}); - // Compress all API responses if appropriate. router.use(compression());