forked from Picmora/nodeapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
164 lines (130 loc) · 3.69 KB
/
app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var express=require("express");
var http=require("http");
var User = require("./models/user");
var mongoose = require("mongoose");
var request=require("request");
var bodyParser=require("body-parser");
var passport = require("passport");
var localStrategy = require("passport-local").Strategy;
var passportLocalMongoose = require("passport-local-mongoose");
var methodOverride = require("method-override");
const flash = require("express-flash-messages");
app.use(flash());
var app=express();
mongoose.connect("mongodb://admin:[email protected]:41530/picmorav1");
app.use(methodOverride("_method"));
app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine","ejs");
//-----------------------------------------------------------------------------------------------------------//
//auth requirements
app.use(require("express-session")({
resave:false,
saveUninitialized:false,
secret:"randomsecret"
}));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(userId, done) {
User.findById(userId, (err, user) => done(err, user));
});
const local = new LocalStrategy((username, password, done) => {
User.findOne({ username })
.then(user => {
if (!user || !user.validPassword(password)) {
done(null, false, { message: "Invalid username/password" });
} else {
done(null, user);
}
})
.catch(e => done(e));
});
passport.use("local", local);
app.use(function(req,res,next){
res.locals.currentUser = req.user;
next();
})
//--------------------------------------------------//
//auth
app.get("/login",isloggedOut,function(req,res){
res.render("login");
});
app.get("/register",function(req,res){
res.render("register")
})
app.post("/register", (req, res, next) => {
const { username, password } = req.body;
User.create({ username, password })
.then(user => {
req.login(user, err => {
if (err) next(err);
else res.redirect("/");
});
})
.catch(err => {
if (err.name === "ValidationError") {
req.flash("Sorry, that username is already taken.");
res.redirect("/register");
} else next(err);
});
});
app.post("/login",passport.authenticate("local", {
successRedirect: "/",
failureRedirect: "/login",
failureFlash: true
})
);
app.get("/logout", function(req, res){
req.logout();
res.redirect("/");
});
// ---------------------------------------------------------//
//auth middleware
const isLoggedIn = (req, res, next) => {
if (req.isAuthenticated()) next();
else res.redirect("/login");
};
const isloggedOut = (req, res, next) => {
if (req.isUnauthenticated()) next();
else res.redirect("/");
};
//---------------------------------------------------------//
//routes
app.get("/",function(req,res){
res.render("index");
});
app.get("/home",isLoggedIn,function(req,res){
res.render("home");
});
app.get("/about",function(req,res){
res.render("about");
});
app.get("/cars",function(req,res){
res.render("cars");
});
app.get("/service",function(req,res){
res.render("service");
});
app.get("/team",function(req,res){
res.render("team");
});
app.get("/blog-home",function(req,res){
res.render("bloghome");
});
app.get("/blog-single",function(req,res){
res.render("blogsingle");
});
app.get("/elements",function(req,res){
res.render("elements");
});
//trash route
app.get("*",function(req,res){
res.render("error");
});
//server
var server = http.createServer(app);
var port = process.env.PORT;
app.set('port', port);
server.listen(port);