Moved members routing+mw into its own app

- create a new app for the /members/ endpoint
- moved all /members/ routes and middleware onto this app
- helps to separate members and frontend/site logic so we can start to decouple things more
This commit is contained in:
Hannah Wolfe 2020-04-30 07:52:22 +01:00
parent 822fda59cb
commit 985bbbd194
4 changed files with 36 additions and 11 deletions

View File

@ -0,0 +1,23 @@
const debug = require('ghost-ignition').debug('web:members:app');
const express = require('../../../shared/express');
const {middleware} = require('../../services/members');
const shared = require('../shared');
module.exports = function setupMembersApp() {
debug('Members setup start');
const membersApp = express();
// Currently global handling for signing in with ?token= magiclinks
membersApp.use(middleware.createSessionFromMagicLink);
// Routing
// Initializes members specific routes as well as assigns members specific data to the req/res objects
membersApp.get('/ssr/member', shared.middlewares.labs.members, middleware.getMemberData);
membersApp.get('/ssr', shared.middlewares.labs.members, middleware.getIdentityToken);
membersApp.delete('/ssr', shared.middlewares.labs.members, middleware.deleteSession);
membersApp.post('/webhooks/stripe', shared.middlewares.labs.members, middleware.stripeWebhooks);
return membersApp;
};

View File

@ -0,0 +1 @@
module.exports = require('./app');

View File

@ -15,8 +15,7 @@ const sitemapHandler = require('../../../frontend/services/sitemap/handler');
const appService = require('../../../frontend/services/apps');
const themeService = require('../../../frontend/services/themes');
const themeMiddleware = themeService.middleware;
const membersService = require('../../services/members');
const membersMiddleware = membersService.middleware;
const membersMiddleware = require('../../services/members').middleware;
const siteRoutes = require('./routes');
const shared = require('../shared');
const mw = require('./middleware');
@ -130,15 +129,7 @@ module.exports = function setupSiteApp(options = {}) {
themeService.loadCoreHelpers();
debug('Helpers done');
// Members middleware
// Initializes members specific routes as well as assigns members specific data to the req/res objects
siteApp.get('/members/ssr/member', shared.middlewares.labs.members, membersMiddleware.getMemberData);
siteApp.get('/members/ssr', shared.middlewares.labs.members, membersMiddleware.getIdentityToken);
siteApp.delete('/members/ssr', shared.middlewares.labs.members, membersMiddleware.deleteSession);
siteApp.post('/members/webhooks/stripe', shared.middlewares.labs.members, membersMiddleware.stripeWebhooks);
// Currently global handling for signing in with ?token= magiclinks
siteApp.use('/members/', membersMiddleware.createSessionFromMagicLink);
siteApp.use('/members', require('../members')());
// Global handling for member session, ensures a member is logged in to the frontend
siteApp.use(membersMiddleware.loadMemberSession);

View File

@ -55,6 +55,11 @@ describe('Basic Members Routes', function () {
.expect(204);
});
it('should error for invalid member token on member data endpoint', function () {
return request.get('/members/ssr/member')
.expect(400);
});
it('should error serving webhook endpoint without any parameters', function () {
return request.post('/members/webhooks/stripe')
.expect(400);
@ -97,6 +102,11 @@ describe('Basic Members Routes', function () {
.expect(404);
});
it('should not serve member data endpoint', function () {
return request.get('/members/ssr/member')
.expect(404);
});
it('should not serve webhook endpoint', function () {
return request.post('/members/webhooks/stripe')
.expect(404);