binds all API routes to api prefix route, refactors to support
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
const passport = require('passport');
|
||||
const AnonymousStrategy = require('passport-anonymous');
|
||||
const jwtStrategy = require('./jwt');
|
||||
const initRoutes = require('../routes/init');
|
||||
const authRoutes = require('./routes');
|
||||
|
||||
const initUserAuth = (User, config, router) => {
|
||||
passport.use(User.createStrategy());
|
||||
|
||||
passport.use(jwtStrategy(User, config));
|
||||
passport.serializeUser(User.serializeUser());
|
||||
passport.deserializeUser(User.deserializeUser());
|
||||
|
||||
passport.use(new AnonymousStrategy.Strategy());
|
||||
|
||||
router.use('', initRoutes(User));
|
||||
router.use('', authRoutes(config, User));
|
||||
};
|
||||
|
||||
module.exports = initUserAuth;
|
||||
31
src/auth/register.js
Normal file
31
src/auth/register.js
Normal file
@@ -0,0 +1,31 @@
|
||||
const mongoose = require('mongoose');
|
||||
const passport = require('passport');
|
||||
const AnonymousStrategy = require('passport-anonymous');
|
||||
const passportLocalMongoose = require('passport-local-mongoose');
|
||||
const jwtStrategy = require('./jwt');
|
||||
const authRoutes = require('./routes');
|
||||
const buildCollectionSchema = require('../collections/buildSchema');
|
||||
const baseUserFields = require('../auth/baseFields');
|
||||
const collectionRoutes = require('../collections/routes');
|
||||
|
||||
function registerUser() {
|
||||
this.config.user.fields.push(...baseUserFields);
|
||||
const userSchema = buildCollectionSchema(this.config.user, this.config);
|
||||
userSchema.plugin(passportLocalMongoose, { usernameField: this.config.user.auth.useAsUsername });
|
||||
this.User = mongoose.model(this.config.user.labels.singular, userSchema);
|
||||
|
||||
passport.use(this.User.createStrategy());
|
||||
passport.use(jwtStrategy(this.User, this.config));
|
||||
passport.serializeUser(this.User.serializeUser());
|
||||
passport.deserializeUser(this.User.deserializeUser());
|
||||
passport.use(new AnonymousStrategy.Strategy());
|
||||
|
||||
this.router.use(authRoutes(this.config, this.User));
|
||||
|
||||
this.router.use(collectionRoutes({
|
||||
model: this.User,
|
||||
config: this.config.user,
|
||||
}));
|
||||
}
|
||||
|
||||
module.exports = registerUser;
|
||||
@@ -8,6 +8,17 @@ const router = express.Router();
|
||||
const authRoutes = (config, User) => {
|
||||
const auth = authRequestHandlers(config, 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 });
|
||||
});
|
||||
});
|
||||
|
||||
router
|
||||
.route('/login')
|
||||
.post(auth.login);
|
||||
|
||||
Reference in New Issue
Block a user