// ====== Dependencies
var express = require('express');
bodyParser = require('body-parser');
validator = require('express-validator');
cookieParser = require('cookie-parser');
expressSession = require('express-session');
passport = require('passport');
LocalStrategy = require('passport-local').Strategy;
auth = require('./controllers/auth');
registration = require('./controllers/register');
User = require('./models/user');
// ====== Options
var app = express()
.use(bodyParser.urlencoded({ extended: false }))
.use(bodyParser.json())
.use(validator())
.set('view engine','ejs')
.use(express.static("views"))
.use(cookieParser())
.use(expressSession({
secret : 'soooosecret',
resave : true, saveUninitialized : false
}))
.use(passport.initialize())
.use(passport.session());
passport.use(new LocalStrategy(User.authenticate())); passport.serializeUser(User.serializeUser()); passport.deserializeUser(User.deserializeUser()); app.post('/register', passport.authenticate('local'), registration.reg );
app.listen(8080);
console.log('Server running at 8080');
var User = require('../models/user');
var reg = function (req, res) {
/* some form validation... */
var errors = req.validationErrors();
if (errors) {
res.render("register" , { errors : errors } );
} else {
var newUser = new User({
username : username,
password : password,
email : email,
tel : tel,
country : country
});
User.createUser(newUser);
}
}
module.exports = { reg : reg }
var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/mvpem');
var passportLocalMongoose = require('passport-local-mongoose'); var db = mongoose.connection;
var Schema = mongoose.Schema;
var UserSchema = new Schema({
username : String,
password : String,
email : String,
tel : Number,
country : String
});
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model('User', UserSchema); module.exports.createUser = function(newUser) {
newUser.save();
}