-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
116 lines (95 loc) · 2.74 KB
/
server.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
/**
* Created by abhiroop on 5/6/14.
*/
var express = require('express'),
http = require('http'),
passport = require('passport'),
morgan = require('morgan'),
compress = require('compression'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
LocalStrategy = require('passport-local').Strategy,
serverStatic = require('serve-static'),
CHAMPIONS = require('./champions').CHAMPIONS;
var USER = {username: 'admin', password: 'admin'};
var app = express();
app.use(morgan());
app.use(compress());
app.use(bodyParser());
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
app.use(methodOverride());
app.use(cookieParser());
app.use(session({
secret : 'almvnirtgd#$DFsa25452*AYD*D*S!@!#adsda))Ddsadsax',
cookie: {httpOnly: true, secure: false, maxAge: 86400000},
store: new session.MemoryStore()
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(function(username, password, done) {
if (USER.username === username) {
if (password === USER.password) {
done(null, USER);
} else {
done(null, false, {msg: 'Incorrect password'});
}
} else {
done(null, false, {msg: 'Could not find user with username ' + username});
}
}));
app.use('/', serverStatic(__dirname + '/app'));
var isLoggedIn = function(req, res, next) {
if (req.isAuthenticated()) {
next();
} else {
res.send({
msg: 'Please login to access this information'
}, 400);
}
};
app.get('/api/details', function(req, res) {
res.send(CHAMPIONS.DETAILS);
});
app.get('/api/topteam', function(req, res) {
res.send(CHAMPIONS.TOP_TEAM);
});
app.get('/api/topscorer', function(req, res) {
res.send(CHAMPIONS.TOP_SCORER);
});
app.get('/api/mostapp', function(req, res) {
res.send(CHAMPIONS.MOST_APP);
});
app.post('/api/login', function(req, res, next) {
passport.authenticate('local', function(err, user) {
if (err) {return next(err); }
if (!user) { return res.send({loginStatus: false, msg: 'Unable to login'}, 400); }
req.logIn(user, function(err) {
if (err) { return res.send({msg: 'Error logging in', err: err}, 500); }
return res.send({loginStatus: true, user: user});
});
})(req, res, next);
});
app.get('/api/session', isLoggedIn, function(req, res) {
res.send({
loginStatus: true,
user: req.user
});
});
app.get('/api/team/:club', function(req, res) {
var club = req.params.club;
res.send(CHAMPIONS.TEAM_DETAILS[club]);
});
app.get('/api/logout', function(req, res) {
req.logout();
res.redirect('/#/login');
});
var port = process.env.PORT || 8000;
app.listen(port);
console.log('Please go to http://localhost:' + port);