-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
159 lines (126 loc) · 4.24 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
const express = require('express'); //express package
const bodyParser = require('body-parser'); // body-parser module parses the JSON, buffer, string and URL encoded data submitted using HTTP POST request
const ejs = require('ejs');
const mongoose =require('mongoose'); //mongodb middleware
const session = require('express-session'); //current session to stay login
const passport = require('passport')// for authentication
const passportLocalMongoose = require('passport-local-mongoose'); // middleware for mongodb and passport
const e = require('express');
const app = express();
app.use(express.static('public'));
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({extended:true})); //Parses the text as URL encoded data, and gives the result on req
// all this is form the documentation
app.use(session({
secret: "Odfgnkan .",
resave: false,
saveUninitialized:false
}));//It will create the cookie so that once logged in we don't have to login again unless we close the browser or logOut
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect('mongodb://localhost:27017/userDB',{useNewUrlParser:true, useUnifiedTopology:true}) //setting up mongodb with mongoose middleware
mongoose.set('useCreateIndex',true);
const userSchema = new mongoose.Schema ({
email: String,
password: String,
name: String
}); // schema for mongodb
userSchema.plugin(passportLocalMongoose); //to hash and save the password in the mongodb local database
const User = new mongoose.model('User',userSchema);//creating user model
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser()); // creat a cookie and store all the login user info
passport.deserializeUser(User.deserializeUser());
app.get('/',function (req,res) {
res.render('home');
});
app.get('/phistory',function (req,res) {
if(req.isAuthenticated()){
res.render('phistory',{name: req.user.name});
}else{
res.redirect('/login');
}
});
app.get('/gatepass',function (req,res) {
if(req.isAuthenticated()){
res.render('gatepass',{name: req.user.name});
}else{
res.redirect('/login');
}
});
app.get('/classlink',function (req,res) {
if(req.isAuthenticated()){
res.render('classlink',{name: req.user.name});
}else{
res.redirect('/login');
}
});
app.get('/payment',function (req,res) {
if(req.isAuthenticated()){
res.render('payment',{name: req.user.name});
}else{
res.redirect('/login');
}
});
app.get('/mentor',function (req,res) {
if(req.isAuthenticated()){
res.render('mentor',{name: req.user.name});
}else{
res.redirect('/login');
}
});
app.get('/task2',function (req,res) {
if(req.isAuthenticated()){
res.render('task2',{name:req.user.name});
}else{
res.redirect('/login');
}
});
app.get('/login',function (req,res) {
res.render('login');
});
app.get('/register',function (req,res) {
res.render('register');
});
app.get('/main',function (req,res) {
//to check if user is logged in
if(req.isAuthenticated()){
res.render('main',{name: req.user.name});
}else{
res.redirect('/login');
}
});
app.get('/logout',function (req,res) {//request response
req.logout();
res.redirect('/');
});
app.post('/register',function (req,res) {
//passport local monggoose as middleman
User.register({username: req.body.username ,name: req.body.name}, req.body.password, function(err,user) {//as a javscript object
if(err){
console.log(err);
res.redirect('/register');
} else{
passport.authenticate("local")(req,res, function () {
res.redirect('/main');
});
}
});
});
app.post('/login', function (req,res) {
const user = new User({
username: req.body.username,
password: req.body.password
});
req.login(user, function (err) { // this method comes from passport
if (err){
console.log(err);
}else{
passport.authenticate("local")(req,res ,function () {
res.redirect('/main',);
})
}
})
});
app.listen(3000,function () {
console.log("Server started in port 3000.")
});