-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·148 lines (95 loc) · 3.01 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
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
let bodyParser = require('body-parser');
let express = require('express');
let app = express();
let User = require('./models/user');
let Post = require('./models/post');
let { check, validationResult } = require('express-validator/check');
let util = require('util');
let flash=require("connect-flash");
app.use(flash());
let passport = require('passport')
let session = require('express-session')
let LocalStrategy = require('passport-local').Strategy;
// MIDDLEWARES
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// CONFIGURE SESSION AND AUTHENTICATION
passport.serializeUser((user, done) =>{
done(null, user);
});
passport.deserializeUser((user, done) =>{
done(null, user);
});
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
(username, password, done) =>{
User.findOne({ username: username }, (err, user) =>{
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
});
}
));
app.use(session({
secret: 'qdfkdslfkmqlk',
resave: false,
saveUninitialized: true
}));
app.use(passport.initialize())
app.use(passport.session())
// -------------------- Template engine ---------------------
app.set('view engine','ejs');
app.use(express.static('public'));
// ----------------- ROUTES -------------------------
// ------------------- GET ---------------------------
app.get('/', (request, response)=>{
let posts = Post.all((posts)=>{ response.render('index',{username:request.user!==undefined?request.user.row.firstname:undefined,posts:posts});})
});
app.get('/newpost/', (request, response)=>{
if(!request.isAuthenticated()) response.redirect('/login');
else{
response.render('newpost');
}
});
app.get('/login', (request, response)=>{
response.render('login');
});
app.get('/subscribe', (request, response)=>{
response.render('subscribe');
});
// ---------------------- POST --------------------------
app.post('/login',passport.authenticate('local',{successRedirect:'/',failureRedirect:'/login',failureFlash:true}));
app.post('/newpost', (request, response)=>{
if(!request.isAuthenticated()) response.redirect('/login');
else{
Post.send(request.user.row.id, request.user.row.firstname ,request.body, ()=>{
console.log('Post posted !');
response.redirect('/');
})
}});
app.post('/subscribe', [
check('email').isEmail(),
check('password').isLength({ min: 5 })
]
, (request, response)=>{
let errors=validationResult(request);
if(!errors.isEmpty()) response.render('index',{error:'Error in form'});
else{
r={
firstname:request.body['first-name'],
lastname:request.body['last-name'],
password:request.body['password'],
email:request.body['email'],
}
user.subscribe(r, ()=>{
response.render('index',{success:'Subscribed successfully !'})
})
}});
app.listen(8080);