-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·101 lines (89 loc) · 3.44 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
var path = require('path');
var qs = require('querystring');
var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser');
var request = require('request');
var mongoose = require('mongoose');
var async = require("async");
var Players = require('./api/models/fantasyModel');
var fantasy = require('./data/fantasy');
var clientId = process.env.APP_CLIENT_ID || require('./conf.js').APP_CLIENT_ID;
var clientSecret = process.env.APP_CLIENT_SECRET || require('./conf.js').APP_CLIENT_SECRET;
var redirectUri = process.env.APP_REDIRECT_URI || require('./conf.js').APP_CLIENT_URL;
//setup the express app
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.set('port', process.env.PORT || 3000);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false })); //should this be true?
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
next();
});
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true
})); //Not sure what this is
app.use(express.static(path.join(__dirname, 'public')));
// mongoose instance connection url connection
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/fantasydb');
var routes = require('./api/routes/fantasyRoutes'); //importing route
routes(app); //register the route
app.get('/', function(req, res) {
});
app.get('/logout', function(req, res) {
delete req.session.token;
res.clearCookie("leagueId");
res.clearCookie("teamId");
res.clearCookie("fantasyPlatform");
res.clearCookie("yahooAccessToken");
res.clearCookie("teamSelected");
res.clearCookie("teamSelectedLabel");
res.clearCookie("teamSelectedValue");
res.clearCookie("yahooAccessToken");
res.clearCookie("teamStatsRecentAvg");
res.clearCookie("teamStatsSeasonAvg");
res.clearCookie("teamTradeSelectedLabel");
res.clearCookie("teamTradeSelectedValue");
res.clearCookie("yahooEmail");
res.clearCookie("paid");
res.clearCookie("dataExpireDate");
res.clearCookie("teamTradeStatsSeason");
res.redirect('/');
});
//go to yahoo auth and get back the client data
app.get('/auth/yahoo', function(req, res) {
var authorizationUrl = 'https://api.login.yahoo.com/oauth2/request_auth';
var queryParams = qs.stringify({
client_id: clientId,
redirect_uri: redirectUri,
response_type: 'code'
});
res.redirect(authorizationUrl + '?' + queryParams);
});
//this is the callback from yahoo. grab the headers and query all the data
app.get('/auth/yahoo/callback', function(req, res) {
var accessTokenUrl = 'https://api.login.yahoo.com/oauth2/get_token';
var options = {
url: accessTokenUrl,
headers: { Authorization: 'Basic ' + new Buffer(clientId + ':' + clientSecret).toString('base64') },
rejectUnauthorized: false,
json: true,
form: {
code: req.query.code,
redirect_uri: redirectUri,
grant_type: 'authorization_code'
}
};
//Load in all the data
fantasy.getYahooData(req, res, options);
});
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});