From d589a90a42e329baf22924bc82e3540258222615 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 5 Oct 2020 17:08:59 -0400 Subject: [PATCH] allows for local payload initialization --- src/collections/init.js | 147 ++++++++++++++++++++-------------------- src/globals/init.js | 19 +++--- src/index.js | 97 ++++++++++++++------------ 3 files changed, 140 insertions(+), 123 deletions(-) diff --git a/src/collections/init.js b/src/collections/init.js index 238843f393..cb774fc542 100644 --- a/src/collections/init.js +++ b/src/collections/init.js @@ -73,89 +73,92 @@ function registerCollections() { config: formattedCollection, }; - const router = express.Router(); - const { slug } = collection; + // If not local, open routes + if (!this.config.local) { + const router = express.Router(); + const { slug } = collection; - router.all(`/${slug}*`, bindCollectionMiddleware(this.collections[formattedCollection.slug])); - - const { - create, - find, - update, - findByID, - delete: deleteHandler, - } = this.requestHandlers.collections; - - if (collection.auth) { - const AuthCollection = this.collections[formattedCollection.slug]; - passport.use(new LocalStrategy(AuthCollection.Model.authenticate())); - - if (collection.auth.useAPIKey) { - passport.use(`${AuthCollection.config.slug}-api-key`, apiKeyStrategy(this, AuthCollection)); - } + router.all(`/${slug}*`, bindCollectionMiddleware(this.collections[formattedCollection.slug])); const { - init, - login, - logout, - refresh, - me, - registerFirstUser, - forgotPassword, - resetPassword, - verifyEmail, - } = this.requestHandlers.collections.auth; + create, + find, + update, + findByID, + delete: deleteHandler, + } = this.requestHandlers.collections; + + if (collection.auth) { + const AuthCollection = this.collections[formattedCollection.slug]; + passport.use(new LocalStrategy(AuthCollection.Model.authenticate())); + + if (collection.auth.useAPIKey) { + passport.use(`${AuthCollection.config.slug}-api-key`, apiKeyStrategy(this, AuthCollection)); + } + + const { + init, + login, + logout, + refresh, + me, + registerFirstUser, + forgotPassword, + resetPassword, + verifyEmail, + } = this.requestHandlers.collections.auth; + + if (collection.auth.emailVerification) { + router + .route(`/${slug}/verify/:token`) + .post(verifyEmail); + } - if (collection.auth.emailVerification) { router - .route(`/${slug}/verify/:token`) - .post(verifyEmail); + .route(`/${slug}/init`) + .get(init); + + router + .route(`/${slug}/login`) + .post(login); + + router + .route(`/${slug}/logout`) + .post(logout); + + router + .route(`/${slug}/refresh-token`) + .post(refresh); + + router + .route(`/${slug}/me`) + .get(me); + + router + .route(`/${slug}/first-register`) + .post(registerFirstUser); + + router + .route(`/${slug}/forgot-password`) + .post(forgotPassword); + + router + .route(`/${slug}/reset-password`) + .post(resetPassword); } - router - .route(`/${slug}/init`) - .get(init); + router.route(`/${slug}`) + .get(find) + .post(create); - router - .route(`/${slug}/login`) - .post(login); + router.route(`/${slug}/:id`) + .put(update) + .get(findByID) + .delete(deleteHandler); - router - .route(`/${slug}/logout`) - .post(logout); - - router - .route(`/${slug}/refresh-token`) - .post(refresh); - - router - .route(`/${slug}/me`) - .get(me); - - router - .route(`/${slug}/first-register`) - .post(registerFirstUser); - - router - .route(`/${slug}/forgot-password`) - .post(forgotPassword); - - router - .route(`/${slug}/reset-password`) - .post(resetPassword); + this.router.use(router); } - router.route(`/${slug}`) - .get(find) - .post(create); - - router.route(`/${slug}/:id`) - .put(update) - .get(findByID) - .delete(deleteHandler); - - this.router.use(router); - return formattedCollection; }); } diff --git a/src/globals/init.js b/src/globals/init.js index ef49baa5b5..5a4261c2c9 100644 --- a/src/globals/init.js +++ b/src/globals/init.js @@ -8,16 +8,19 @@ function initGlobals() { config: this.config.globals, }; - const router = express.Router(); + // If not local, open routes + if (!this.config.local) { + const router = express.Router(); - this.config.globals.forEach((global) => { - router - .route(`/globals/${global.slug}`) - .get(this.requestHandlers.globals.findOne(global)) - .post(this.requestHandlers.globals.update(global)); - }); + this.config.globals.forEach((global) => { + router + .route(`/globals/${global.slug}`) + .get(this.requestHandlers.globals.findOne(global)) + .post(this.requestHandlers.globals.update(global)); + }); - this.router.use(router); + this.router.use(router); + } } } diff --git a/src/index.js b/src/index.js index 86450a9bf1..900fc3c79d 100644 --- a/src/index.js +++ b/src/index.js @@ -46,12 +46,11 @@ class Payload { email, secret: options.secret, mongoURL: options.mongoURL, + local: options.local, }); if (typeof this.config.paths === 'undefined') this.config.paths = {}; - this.express = options.express; - this.router = express.Router(); this.collections = {}; bindOperations(this); @@ -68,47 +67,6 @@ class Payload { this.initAdmin = initAdmin.bind(this); this.performFieldOperations = performFieldOperations.bind(this); - // Configure email service - this.email = this.buildEmail(); - - // Setup & initialization - connectMongoose(this.config.mongoURL); - - this.router.use(...expressMiddleware(this)); - - this.initAuth(); - this.initCollections(); - this.initGlobals(); - this.initAdmin(); - - this.router.get('/access', this.requestHandlers.collections.auth.access); - - const graphQLHandler = new GraphQL(this); - - this.router.use( - this.config.routes.graphQL, - identifyAPI('GraphQL'), - (req, res) => graphQLHandler.init(req, res)(req, res), - ); - - this.router.get(this.config.routes.graphQLPlayground, graphQLPlayground({ - endpoint: `${this.config.routes.api}${this.config.routes.graphQL}`, - settings: { - 'request.credentials': 'include', - }, - })); - - // Bind router to API - this.express.use(this.config.routes.api, this.router); - - // Enable static routes for all collections permitting upload - this.initStatic(); - - this.errorHandler = errorHandler(this.config); - this.router.use(this.errorHandler); - - this.authenticate = authenticate(this.config); - this.create = this.create.bind(this); this.find = this.find.bind(this); this.findGlobal = this.findGlobal.bind(this); @@ -119,6 +77,59 @@ class Payload { this.forgotPassword = this.forgotPassword.bind(this); this.resetPassword = this.resetPassword.bind(this); + // If not initializing locally, scaffold router + if (!this.config.local) { + this.router = express.Router(); + this.router.use(...expressMiddleware(this)); + this.initAuth(); + } + + // Configure email service + this.email = this.buildEmail(); + + // Initialize collections & globals + this.initCollections(); + this.initGlobals(); + + // Connect to database + connectMongoose(this.config.mongoURL); + + + // If not initializing locally, set up HTTP routing + if (!this.config.local) { + this.express = options.express; + + this.initAdmin(); + + this.router.get('/access', this.requestHandlers.collections.auth.access); + + const graphQLHandler = new GraphQL(this); + + this.router.use( + this.config.routes.graphQL, + identifyAPI('GraphQL'), + (req, res) => graphQLHandler.init(req, res)(req, res), + ); + + this.router.get(this.config.routes.graphQLPlayground, graphQLPlayground({ + endpoint: `${this.config.routes.api}${this.config.routes.graphQL}`, + settings: { + 'request.credentials': 'include', + }, + })); + + // Bind router to API + this.express.use(this.config.routes.api, this.router); + + // Enable static routes for all collections permitting upload + this.initStatic(); + + this.errorHandler = errorHandler(this.config); + this.router.use(this.errorHandler); + + this.authenticate = authenticate(this.config); + } + if (typeof options.onInit === 'function') options.onInit(); }