feature(first-register): add first-register routes and pull out auth.routes.js
This commit is contained in:
34
src/index.js
34
src/index.js
@@ -7,24 +7,22 @@ import jwtStrategy from './auth/jwt';
|
||||
import fileUpload from 'express-fileupload';
|
||||
import {upload as uploadMedia, update as updateMedia} from './media/requestHandlers';
|
||||
import mediaConfig from './media/media.config';
|
||||
import passwordResetRoutes from './auth/passwordResets/passwordReset.routes';
|
||||
import initRoutes from './routes/init.routes';
|
||||
import autopopulate from './mongoose/autopopulate.plugin';
|
||||
import paginate from './mongoose/paginate.plugin';
|
||||
import buildQueryPlugin from './mongoose/buildQuery.plugin';
|
||||
import localizationPlugin from './localization/localization.plugin';
|
||||
import bindModelMiddleware from './mongoose/bindModel.middleware';
|
||||
import localizationMiddleware from './localization/localization.middleware';
|
||||
import checkRoleMiddleware from './auth/checkRole.middleware';
|
||||
import { query, create, findOne, destroy, update } from './mongoose/requestHandlers';
|
||||
import { upsert, fetch } from './mongoose/requestHandlers/globals';
|
||||
import { schemaBaseFields } from './mongoose/schemaBaseFields';
|
||||
import fieldToSchemaMap from './mongoose/fieldToSchemaMap';
|
||||
import authValidate from './auth/validate';
|
||||
import authRequestHandlers from './auth/requestHandlers';
|
||||
import passwordResetConfig from './auth/passwordResets/passwordReset.config';
|
||||
import validateCollection from './utilities/validateCollection';
|
||||
import validateGlobal from './utilities/validateGlobal';
|
||||
import setModelLocaleMiddleware from './mongoose/setModelLocale.middleware';
|
||||
import authRoutes from './routes/auth.routes';
|
||||
|
||||
class Payload {
|
||||
|
||||
@@ -118,32 +116,8 @@ class Payload {
|
||||
passport.deserializeUser(model.deserializeUser());
|
||||
}
|
||||
|
||||
let auth = authRequestHandlers(model);
|
||||
|
||||
options.router
|
||||
.route('/login')
|
||||
.post(authValidate.login, auth.login);
|
||||
|
||||
options.router
|
||||
.route('/me')
|
||||
.post(passport.authenticate(config.auth.strategy, { session: false }), auth.me);
|
||||
|
||||
options.config.roles.forEach((role) => {
|
||||
options.router
|
||||
.route(`/role/${role}`)
|
||||
.get(passport.authenticate(config.auth.strategy, { session: false }), checkRoleMiddleware(role), auth.me);
|
||||
});
|
||||
|
||||
// password resets
|
||||
if (config.auth.passwordResets) {
|
||||
options.router.use('', passwordResetRoutes(options.config.email, model));
|
||||
}
|
||||
|
||||
if (config.auth.registration) {
|
||||
options.router
|
||||
.route(`${config.slug}/register`) // TODO: not sure how to incorporate url params like `:pageId`
|
||||
.post(config.auth.registrationValidation, auth.register);
|
||||
}
|
||||
options.router.use('', initRoutes(model));
|
||||
options.router.use('', authRoutes(config, model));
|
||||
}
|
||||
|
||||
options.router.all(`/${config.slug}*`,
|
||||
|
||||
54
src/routes/auth.routes.js
Normal file
54
src/routes/auth.routes.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import express from 'express';
|
||||
import authRequestHandlers from '../auth/requestHandlers';
|
||||
import authValidate from '../auth/validate';
|
||||
import passport from 'passport';
|
||||
import checkRoleMiddleware from '../auth/checkRole.middleware';
|
||||
import passwordResetRoutes from './passwordReset.routes';
|
||||
|
||||
const router = express.Router();
|
||||
const authRoutes = (userConfig, User) => {
|
||||
|
||||
let auth = authRequestHandlers(User);
|
||||
|
||||
router
|
||||
.route('/login')
|
||||
.post(authValidate.login, auth.login);
|
||||
|
||||
router
|
||||
.route('/me')
|
||||
.post(passport.authenticate(userConfig.auth.strategy, { session: false }), auth.me);
|
||||
|
||||
userConfig.roles.forEach((role) => {
|
||||
router
|
||||
.route(`/role/${role}`)
|
||||
.get(passport.authenticate(userConfig.auth.strategy, { session: false }), checkRoleMiddleware(role), auth.me);
|
||||
});
|
||||
|
||||
if (userConfig.auth.passwordResets) {
|
||||
router.use('', passwordResetRoutes(userConfig.email, User));
|
||||
}
|
||||
|
||||
if (userConfig.auth.registration) {
|
||||
router
|
||||
.route(`${userConfig.slug}/register`) // TODO: not sure how to incorporate url params like `:pageId`
|
||||
.post(userConfig.auth.registrationValidation, auth.register);
|
||||
|
||||
router
|
||||
.route('/first-register')
|
||||
.post(userConfig.auth.registrationValidation,
|
||||
(req, res, next) => {
|
||||
User.countDocuments({}, (err, count) => {
|
||||
if (err) res.status(500).json({ error: err });
|
||||
if (count >= 1)
|
||||
return res.status(403).json({ initialized: true });
|
||||
next();
|
||||
})
|
||||
},
|
||||
auth.register
|
||||
);
|
||||
}
|
||||
|
||||
return router;
|
||||
};
|
||||
|
||||
export default authRoutes;
|
||||
20
src/routes/init.routes.js
Normal file
20
src/routes/init.routes.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import express from 'express';
|
||||
|
||||
const router = express.Router();
|
||||
const initRoutes = User => {
|
||||
|
||||
router
|
||||
.route('/init')
|
||||
.get((req, res) => {
|
||||
User.countDocuments({}, (err, count) => {
|
||||
if (err) res.status(200).json({ error: err });
|
||||
return count >= 1
|
||||
? res.status(200).json({ initialized: true })
|
||||
: res.status(200).json({ initialized: false })
|
||||
})
|
||||
});
|
||||
|
||||
return router;
|
||||
};
|
||||
|
||||
export default initRoutes;
|
||||
@@ -1,5 +1,5 @@
|
||||
import express from 'express';
|
||||
import passport from 'passport';
|
||||
import passport from 'passport/lib';
|
||||
import * as nodemailer from 'nodemailer';
|
||||
import * as crypto from 'crypto';
|
||||
|
||||
Reference in New Issue
Block a user