-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
145 lines (117 loc) · 3.71 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
//IMPORTS
const express = require('express')
const app = express()
var path = require('path');
//STATIC FILES
app.use(express.static('public'))
app.use('/css',express.static(__dirname + 'public/css'));
app.use('/js',express.static(__dirname + 'public/src'));
app.use('/images',express.static(__dirname + 'public/css'));
app.get('' , (req, res) =>{
res.sendFile(__dirname + '/layout/index.html')
})
app.get('/layout/index.html' , (req, res) =>{
res.sendFile(__dirname + '/layout/index.html')
})
app.get('/layout/services.html', (req, res) =>{
res.sendFile(__dirname + '/layout/services.html')
})
app.get('/layout/articles.html', (req, res) =>{
res.sendFile(__dirname + '/layout/articles.html')
})
app.get('/layout/diets.html', (req, res) =>{
res.sendFile(__dirname + '/layout/diets.html')
})
app.get('/layout/bookonline.html', (req, res) =>{
res.sendFile(__dirname + '/layout/bookonline.html')
})
app.get('/action_page.php' , (req, res) =>{
})
const exphbs = require("express-handlebars"),
mongoose = require("mongoose"),
passport = require("passport"),
bodyParser = require("body-parser"),
LocalStrategy = require("passport-local"),
passportLocalMongoose = require("passport-local-mongoose"),
User = require("./models/user");
//Connecting database
mongoose.connect("mongodb://localhost/auth_demo",{useNewUrlParser: true, useUnifiedTopology: true });
app.use(require("express-session")({
secret:"Any normal Word", //decode or encode session
resave: false,
saveUninitialized:false
}));
passport.serializeUser(User.serializeUser()); //session encoding
passport.deserializeUser(User.deserializeUser()); //session decoding
passport.use(new LocalStrategy(User.authenticate()));
app.engine("hbs",exphbs({
extname: "hbs",
defaultLayout: false
})
);
// app.engine('hbs', exphbs({ extname: '.hbs' }));
app.set("view engine","hbs");
app.use(bodyParser.urlencoded(
{ extended:true }
))
app.use(passport.initialize());
app.use(passport.session());
//=======================
// R O U T E S
//=======================
app.use(express.static(__dirname + '/public'));
app.get("/", (req,res) =>{
res.render("home");
})
app.get("/userprofile",isLoggedIn ,(req,res) =>{
res.render("userprofile");
})
//Auth Routes
app.get("/login",(req,res)=>{
res.render("login");
});
app.post("/login",passport.authenticate("local",{
successRedirect:"/userprofile",
failureRedirect:"/login"
}),function (req, res){
});
app.get("/register",(req,res)=>{
res.render("register");
});
app.post("/register",(req,res)=>{
if(req.body.username === '' || req.body.password === '' || req.body.phone === '' || req.body.email === '' ){
res.redirect("/register");
}
else{
User.register(new User({username: req.body.username,phone:req.body.phone,email: req.body.email}),req.body.password,function(err,user){
if(err){
console.log(err);
res.render("register",{
message: 'That username is already in use'
});
}else{
passport.authenticate("local")(req,res,function(){
res.redirect("/login");
});
}
})
}
})
app.get("/logout",(req,res)=>{
req.logout();
res.redirect("/");
});
function isLoggedIn(req,res,next) {
if(req.isAuthenticated()){
return next();
}
res.redirect("/login");
}
//Listen On Server
app.listen(process.env.PORT ||3000,function (err) {
if(err){
console.log(err);
}else {
console.log("Server Started At Port 3000");
}
});