Created
July 16, 2018 02:53
-
-
Save yukeehan/23fbbe53f1ca94be440161c1562b489a to your computer and use it in GitHub Desktop.
Login/Logout/Register using Passport-Local-Mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require("express"), | |
mongoose = require("mongoose"), | |
bodyParser = require("body-parser"), | |
User = require("./models/user"), | |
passport = require("passport"), | |
LocalStrategy = require("passport-local"), | |
passportLocalMongoose = require("passport-local-mongoose"); | |
mongoose.connect("mongodb://localhost:27017/auth_demo_app", { useNewUrlParser: true }); | |
var app = express(); | |
app.set("view engine","ejs"); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(require("express-session")({ | |
secret:"Miss white is my cat", | |
resave: false, | |
saveUninitialized: false | |
})); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
passport.use(new LocalStrategy(User.authenticate())); | |
passport.serializeUser(User.serializeUser()); | |
passport.deserializeUser(User.deserializeUser()); | |
// =================== | |
// ROUTES | |
// =================== | |
app.get("/",function(req, res){ | |
res.render("home"); | |
}); | |
app.get("/secret", isLoggedIn, function(req, res){ | |
res.render("secret"); | |
}); | |
app.get("/register", function(req, res){ | |
res.render("register"); | |
}); | |
// handeling user sign up | |
app.post("/register", function(req, res){ | |
// console.log(req.body.username); | |
// console.log(req.body.password); | |
User.register(new User({username: req.body.username}), req.body.password, function(err, user){ | |
if(err){ | |
console.log(err); | |
return res.render("register"); | |
} | |
passport.authenticate("local")(req, res, function(){ | |
res.redirect("/secret"); | |
}); | |
}); | |
}); | |
// Login Form | |
app.get("/login", function(req, res){ | |
res.render("login"); | |
}); | |
// Login Logic | |
// middleware | |
app.post("/login", passport.authenticate("local",{ | |
successRedirect: "/secret", | |
failureRedirect: "/login" | |
}), function(req, res){ | |
}); | |
// Logout | |
app.get("/logout", function(req, res){ | |
req.logout(); | |
res.redirect("/"); | |
}); | |
// check isLoggedIn | |
function isLoggedIn(req, res, next){ | |
if(req.isAuthenticated()){ | |
return next(); | |
} | |
res.redirect("/login"); | |
} | |
app.listen(process.env.PORT, process.env.IP, function(){ | |
console.log("app started!!!") | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>This is the homepage!</h1> | |
<li><a href="/register">register!</a></li> | |
<li><a href="/login">login!</a></li> | |
<li><a href="/logout">logout!</a></li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Login Page</h1> | |
<form action="/login" method="POST"> | |
<input type="text" name="username" placeholder="username"> | |
<input type="password" name="password" placeholder="password"> | |
<button>Submit</button> | |
</form> | |
<li><a href="/register">register!</a></li> | |
<li><a href="/login">login!</a></li> | |
<li><a href="/logout">logout!</a></li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Sign Up Form</h1> | |
<form action="/register" method="POST"> | |
<input type="text" name="username" placeholder="username"> | |
<input type="password" name="password" placeholder="password"> | |
<button>Submit</button> | |
</form> | |
<li><a href="/register">register!</a></li> | |
<li><a href="/login">login!</a></li> | |
<li><a href="/logout">logout!</a></li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Secret page</h1> | |
<li><a href="/register">register!</a></li> | |
<li><a href="/login">login!</a></li> | |
<li><a href="/logout">logout!</a></li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require("mongoose"); | |
var passportLocalMongoose = require("passport-local-mongoose"); | |
var UserSchema = new mongoose.Schema({ | |
username: String, | |
password: String | |
}); | |
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
userSchema.plugin(passportLocalMongoose); should be added in order for User.authenticate() to work:
'./models/user'