-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (67 loc) · 2.05 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
if (!process.browser) global.fetch = require("isomorphic-unfetch");
const express = require("express");
const next = require("next");
const request = require("request");
const cors = require("cors");
const querystring = require("querystring");
const cookieParser = require("cookie-parser");
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev, quiet: true });
const handle = app.getRequestHandler();
const { CLIENT_ID, CLIENT_SECRET, REDIRECT_URI } = process.env;
app.prepare().then(() => {
const server = express();
server.use(cors());
server.use(cookieParser());
server.get("/get_spotify_uri", function(req, res) {
const scopes = [
"user-read-private",
"user-read-email",
"playlist-read-private",
"playlist-read-collaborative"
];
const spotify_uri =
"https://accounts.spotify.com/authorize?" +
querystring.stringify({
show_dialog: true,
response_type: "token",
client_id: CLIENT_ID,
scope: scopes.join(" "),
redirect_uri: REDIRECT_URI
});
res.send({ spotify_uri });
});
server.get("/refresh_token", function(req, res) {
// requesting access token from refresh token
var refresh_token = req.query.refresh_token;
var authOptions = {
url: "https://accounts.spotify.com/api/token",
headers: {
Authorization:
"Basic " +
new Buffer(CLIENT_ID + ":" + CLIENT_SECRET).toString("base64")
},
form: {
grant_type: "refresh_token",
refresh_token: refresh_token
},
json: true
};
request.post(authOptions, function(error, response, body) {
if (!error && response.statusCode === 200) {
var access_token = body.access_token;
res.send({
access_token: access_token
});
}
});
});
server.get("*", (req, res) => {
return handle(req, res);
});
server.listen(port, err => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});