-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.js
57 lines (49 loc) · 1.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var express = require("express");
var router = express.Router();
var passport = require("passport");
var User = require("../models/user");
//root route
router.get("/", function(req, res){
res.render("landing");
});
// show register form
router.get("/register", function(req, res){
res.render("register");
});
//handle sign up logic
router.post("/register", function(req, res){
var newUser = new User({username: req.body.username});
User.register(newUser, req.body.password, function(err, user){
if(err){
console.log(err);
return res.render("register");
}
passport.authenticate("local")(req, res, function(){
res.redirect("/campgrounds");
});
});
});
//show login form
router.get("/login", function(req, res){
res.render("login");
});
//handling login logic
router.post("/login", passport.authenticate("local",
{
successRedirect: "/campgrounds",
failureRedirect: "/login"
}), function(req, res){
});
// logout route
router.get("/logout", function(req, res){
req.logout();
res.redirect("/campgrounds");
});
//middleware
function isLoggedIn(req, res, next){
if(req.isAuthenticated()){
return next();
}
res.redirect("/login");
}
module.exports = router;