-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
164 lines (134 loc) · 4.38 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
const express = require('express')
const path = require('path')
const db = require('./local_modules/database')
var flash=require("connect-flash");
const session = require('express-session');
const bodyParser = require('body-parser');
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;
var customStrategy = require('passport-custom').Strategy;
var pg = require('pg');
var Pool = require('pg-pool')
const config = {
user: 'vvdicwtmvinosq',
password: 'e9179f8811881a2e527c536030e0ac6d4c4eaee84cccbd35f1dd5d7c19769be8',
host: 'ec2-18-235-97-230.compute-1.amazonaws.com',
port: '5432',
database: 'd9p7d0hvpi42ri',
ssl: true
};
const pool = new Pool(config);
const app = express()
const port = process.env.PORT || 8000;
passport.use(new localStrategy(
function(username, password, cb) {
db.users.findByUsername(username, function(err, user) {
if (err) { return cb(err); }
if (!user) { return cb(null, false); }
if (user.password != password) { return cb(null, false); }
return cb(null, user);
});
}));
passport.use('id-only', new customStrategy(
function(req, callback){
var id = req.body.username;
db.users.findByUsername(id, function(err, user){
if (err) {return callback(err)}
if (!user) {return callback(null, false)}
return callback(null, user);
})
}
))
passport.serializeUser((user, done) => {
done(null, user.username);
})
passport.deserializeUser((username, done) => {
//tại đây hứng dữ liệu để đối chiếu
db.users.findByUsername(username, function(err, user) {
if (err) { return done(err); }
done(null, user);
})
});
app.use(bodyParser.urlencoded({extended: true}));
app.use(session({
secret: 'woot',
resave: false,
saveUninitialized: false}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static('public'));
app.use(express.static('view'));
app.use(flash());
app.set('view engine', 'pug')
app.post('/login',
passport.authenticate('id-only', { successRedirect: '/',
failureRedirect: '/login'}));
// var user_sample_data = {
// username: "ngdmau",
// current_income: 100000,
// current_jobs: ['text_sentiment', 'text_topic', 'audio_sentiment'],
// new_jobs: ['audio_topic', 'audio_gender'],
// no_current_jobs: 3,
// no_new_jobs: 2,
// }
app.get('/', function (req, res) {
if (req.isAuthenticated()) {
res.sendFile(path.join(__dirname, '/views/', 'dashboard.html'));
//res.render('dashboard', user_sample_data)
} else {
res.redirect('/login')
}
});
app.get('/dashboard', function (req, res) {
if (req.isAuthenticated()) {
res.sendFile(path.join(__dirname, '/views/', 'dashboard.html'));
} else {
res.redirect('/login')
}
});
app.get('/index', function (req, res) {
if (req.isAuthenticated()) {
res.sendFile(path.join(__dirname, '/views/', 'index.html'));
} else {
res.redirect('/login')
}
});
app.get('/training_audio', function (req, res) {
if (req.isAuthenticated()) {
res.sendFile(path.join(__dirname, '/views/', 'training_audio.html'));
} else {
res.redirect('/login')
}
});
app.get('/training_topic', function (req, res) {
if (req.isAuthenticated()) {
res.sendFile(path.join(__dirname, '/views/', 'training_topic.html'));
} else {
res.redirect('/login')
}
});
app.get('/login', function (req, res) {
if (req.isAuthenticated()) {
res.redirect('/')
} else {
res.sendFile(path.join(__dirname, '/views/', 'login.html'))
}
});
app.get('/about', function (req, res) {
res.sendFile(path.join(__dirname, '/views/', 'about.html'))
});
app.get('/forgotpass', function (req, res) {
res.sendFile(path.join(__dirname, '/views/', 'forgotpass.html'))
});
app.get('/signup', function (req, res) {
res.sendFile(path.join(__dirname, '/views/', 'signup.html'))
});
app.get('/login2', function (req, res) {
res.sendFile(path.join(__dirname, '/views/', 'login2.html'))
});
app.get('/logout', function (req, res){
req.session.destroy(function (err) {
res.redirect('/'); //Inside a callback… bulletproof!
});
});
app.listen(port, () => { console.log(`Example app listening on port ${port}` )});