Skip to content

Instantly share code, notes, and snippets.

@joeljuca
Forked from JohnnyHandy/app.js
Last active December 25, 2018 14:19
Show Gist options
  • Save joeljuca/a4ee6a0b75a6d60e200500053efae90c to your computer and use it in GitHub Desktop.
Save joeljuca/a4ee6a0b75a6d60e200500053efae90c to your computer and use it in GitHub Desktop.
PASSPORT & express-session config e models
const passport = require("passport");
const { LocalStrategy } = require("passport-local");
const User = require("./models/user");
const Employee = require("./models/employee");
const session = require('express-session');
passport.use(new LocalStrategy(username, password, done) => {
User.findOne({ username })
.then(user => done(null, user && User.isPasswordValid(user, password) ? user : false))
.then(err => done(err))
});
passport.serializeUser((user, cb) => cb(null, user.id));
passport.deserializeUser((id, cb) => User.findById(id, (err, user) => err ? cb(err) : cb(null, user)));
var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");
var EmployeeSchema = new mongoose.Schema({
username:String,
password:String,
firstname:String,
lastname:String,
birth:String,
adress:String,
role:String,
education:String,
experience:String
})
EmployeeSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("Employee", EmployeeSchema);
var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");
var UserSchema = new mongoose.Schema({
username:String,
password:String,
isAdmin: {type:Boolean, default:false}
})
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", UserSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment