-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
103 lines (92 loc) · 3.15 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
var express = require('express');
var sqlite = require('sqlite3');
var passport = require('passport');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var LocalStrategy = require('passport-local').Strategy;
var app = express();
var db = new sqlite.Database('db.sqlite');
// Static resources (css, images, js)
app.use(express.static('public'));
app.set('view engine', 'ejs');
// Auth initialize
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(require('express-session')({ secret: 'secret', resave: false, saveUninitialized: false }));
app.use(passport.initialize());
app.use(passport.session());
// Authentication
passport.use(new LocalStrategy(
{
usernameField: 'login',
passwordField: 'password'
},
function(login, password, done) {
db.get('SELECT id, login FROM users WHERE login = ? AND password = ?', login, password, function(err, row) {
console.log("111!!");
if (!row) return done(null, false);
return done(null, row);
});
}));
passport.serializeUser(function(user, done) {
return done(null, user.id);
});
passport.deserializeUser(function(id, done) {
db.get('SELECT id, login FROM users WHERE id = ?', id, function(err, row) {
if (!row) return done(null, false);
return done(null, row);
});
});
app.get('/login', function(request, response){
if (request.isAuthenticated()) {
response.redirect('/');
}
response.render('login.ejs');
});
app.post('/login', passport.authenticate('local',
{session: true, successRedirect: '/', failureRedirect: '/login' }
));
app.post('/signup', function(request, response){
var statement = 'insert into users (login, password) values(?, ?)';
db.run(statement, request.body.login, request.body.password, function () {
response.redirect('login/');
});
});
app.all('/*', function(request, response, next) {
if (!request.isAuthenticated()) {
response.redirect('/login');
}
next(); // call next() here to move on to next middleware/router
});
app.get('/logout', function (request, response) {
request.logout();
response.redirect('/');
});
// Authentication end
app.get('/', function(request, response){
db.all('select * from videos', function(err, data){
response.render('index.ejs', {data: data});
});
});
// Add page & action
app.get('/add', function(request, response){
response.render('add_video.ejs');
});
app.post('/add', function(request, response){
var statement = 'insert into videos (name, link, pic_link) values(?, ?, ?)';
db.run(statement, request.query.video_name, request.query.video_link, request.query.video_pic, function () {
response.send('added');
});
});
app.delete('/delete', function (request, response) {
var statement = 'delete from videos where id = ?';
db.run(statement, request.query.id, function(){
response.send('deleted');
});
});
app.get('/news', function(request, response){
response.render('news.ejs');
});
app.listen(process.env.PORT || 1337, function () {
console.log('App is listening on 1337 local');
});