Skip to content

Instantly share code, notes, and snippets.

@JohnnyHandy
Created December 25, 2018 13:31
Show Gist options
  • Save JohnnyHandy/02e8faa7c68efd371fca09332a9147ca to your computer and use it in GitHub Desktop.
Save JohnnyHandy/02e8faa7c68efd371fca09332a9147ca to your computer and use it in GitHub Desktop.
PASSPORT & express-session config e models
var passportUser = require("passport");
var passportEmployee = require("passport");
var LocalStrategyUser = require("passport-local").Strategy;
var LocalStrategyEmployee = require("passport-local").Strategy;
var User = require("./models/user");
var Employee = require("./models/employee");
var session = require('express-session');
//CONFIGURAÇÃO DO PASSPORT
app.use(session({
secret: "Mar do macaco",
resave: false,
saveUninitialized:false,
cookie : {
secure:false,
expires: false,
}
}));
//USER
app.use(passportUser.initialize());
app.use(passportUser.session());
passportUser.use('user',new LocalStrategyUser(User.authenticate()));
passportUser.serializeUser(function(user, cb) {
cb(null, user.id);
});
passportUser.deserializeUser(function(id, cb) {
User.findById(id, function (err, user) {
if (err) { return cb(err); }
cb(null, user);
});
});
//EMPLOYEE
app.use(passportEmployee.initialize());
app.use(passportEmployee.session());
passportEmployee.use('employee',new
LocalStrategyEmployee(Employee.authenticate()));
Employee.serializeUser(function(user, cb) {
cb(null, user.id);
});
passportEmployee.deserializeUser(function(id, cb) {
User.findById(id, function (err, user) {
if (err) { return cb(err); }
cb(null, user);
});
//======================================================================================================================
//EMPLOYEE LOGIN AND REGISTER ROUTES=======================
//REGISTER NEW EMPLOYEE
app.get("/registernew", function(req,res){
res.render("employeeform")
})
//New Employee register logic
app.post("/registernew", async function(req,res){
var newEmployee = new Employee({
username:req.body.username,
firstname:req.body.firstname,
lastname:req.body.lastname,
birth:req.body.birth,
adress:req.body.adress,
education:req.body.education,
experience:req.body.experience,
role:req.body.role
});
Employee.register(newEmployee, req.body.password,async function(err,employee){
if(err){
console.log(err);
res.render("employeeform");
} else{
console.log("employee registered with success");
}
await passportEmployee.authenticate("employee")(req,res,function(){
console.log("employee logged in");
console.log(req.user.username);
res.redirect("/");
});
})
})
//Employee Login
app.get("/employeelogin", function(req,res){
res.render("employeelogin")
})
app.post("/employeelogin", passportEmployee.authenticate("employee",{successRedirect:"/",failureRedirect:"/employeelogin",failureFlash:true,successFlash: 'Welcome!'}),function(req,res){
console.log(req.user)
})
});
//=======================================================================================================================
//USER LOGIN AND REGISTER ROUTES =========================
//USER REGISTER FORM
app.get("/register", function(req,res){
res.render("register");
});
//handle USER sign up logic
app.post("/register", function(req,res){
var newUser = new User({username:req.body.username});
if(req.body.adminCode === 'secret') {
newUser.isAdmin = true;
}
User.register(newUser, req.body.password, function(err,user){
if(err){
console.log(err);
res.render("register");
}
passportUser.authenticate("user")(req,res, function(){
res.redirect("/");
});
});
});
//SHOW USER LOGIN FORM
app.get("/login", function(req,res){
res.render("login");
})
//handling USER login logic
app.post("/login", passportUser.authenticate("user",{successRedirect:"/",failureRedirect:"/login"}),function(req,res){
})
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