-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
113 lines (92 loc) · 2.53 KB
/
routes.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
var router = require('express').Router();
var querystring = require('querystring');
var db = require('promised-mongo')(process.env.DB_CONNECTION);
var Users = db.collection('users');
var Spotify = require('./modules/spotify');
var checkAuth = function(req, res, next) {
if(Spotify.client.authed) {
console.log('authed');
res.redirect('/playlist/' + Spotify.client.username);
} else {
console.log('not authed');
next();
}
};
router.get('/', function(req, res) {
res.render('index');
});
router.get('/login', function (req, res) {
res.redirect(Spotify.auth());
});
router.post('/users/:username/setPhoneId', function(req, res) {
var id = req.body.phoneId;
var username = req.params.username;
var coords = {
lat: req.body.lat,
lon: req.body.lon
};
Users.update({username: username}, {$set: {
phoneId: id
}})
.then(function() {
return Users.findOne({username: username});
})
.then(function(user) {
return Spotify.updatePlaylist(user, coords);
})
.then(function() {
res.json({status: 'ok'});
});
});
router.post('/users/:username/updateLocation', function(req, res) {
var username = req.params.username;
Users.findOne({username: username}).then(function(user) {
var lon = req.body.lon;
var lat = req.body.lat;
return Spotify.updatePlaylist(user, {
lat: lat,
lon: lon
});
}).then(function() {
res.json({status: 'ok'});
});
});
router.get('/playlist/:username', function(req, res) {
var username = req.params.username;
Users.findOne({username: username}).then(function(user) {
if(!user) {
return res.redirect('/');
}
res.render('playlist', {
user: user,
playlistURI: 'spotify:user:' + user.username + ':playlist:' + user.playlistId
});
});
});
router.post('/update', function(req, res) {
var phoneId = req.body.phoneID;
var coords = {
lat: req.body.latitude,
lon: req.body.longitude
};
Users.findOne({phoneId: phoneId}).then(function(user) {
Spotify.updatePlaylist(user, coords);
})
})
router.get('/callback', function (req, res) {
var state = req.query.state;
var code = req.query.code;
if (state === null || state !== Spotify.state) {
res.redirect('/' +
querystring.stringify({
error: 'state_mismatch'
}));
} else {
Spotify.client.authorizationCodeGrant(code).then(function(data) {
Spotify.createUser(data.body).then(function(user) {
res.redirect('/playlist/' + user.username);
});
});
}
});
module.exports = router;