-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
124 lines (111 loc) · 3.09 KB
/
index.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
117
118
119
120
121
122
123
124
require("dotenv").config();
const request = require("request");
const express = require("express");
const qs = require("querystring");
const app = express();
app.use(express.static("."));
app.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Credentials", true);
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Content-length, Accept, x-access-token"
);
res.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
next();
});
app.get("/auth", (req, res) => {
let config = {
response_type: "code",
client_id: process.env.CLIENT_ID,
redirect_uri: `${process.env.SERVER_URL}/redirect`
};
if (process.env.SCOPES) {
config.scope = process.env.SCOPES;
}
res.redirect(
"https://accounts.spotify.com/authorize?" + qs.stringify(config)
);
});
app.get("/redirect", (req, res) => {
const code = req.url.match(/code=([\w\d-_.]+)/)[1];
const base64Token = `${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`;
request(
{
url: "https://accounts.spotify.com/api/token",
method: "POST",
form: {
grant_type: "authorization_code",
code: code,
redirect_uri: `${process.env.SERVER_URL}/redirect`
},
json: true,
headers: {
Authorization: `Basic ${new Buffer(base64Token).toString("base64")}`
}
},
(err, response, body) => {
res.redirect(`${process.env.APP_URL}?${qs.stringify(body)}`);
}
);
});
app.get("/refresh", (req, res) => {
const base64Token = `${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`;
request(
{
url: "https://accounts.spotify.com/api/token",
method: "post",
form: {
grant_type: "refresh_token",
refresh_token: req.query.refresh_token
},
headers: {
Authorization: `Basic ${new Buffer(base64Token).toString("base64")}`
}
},
(err, response, body) => {
res.send(body);
}
);
});
// Client credentials flow - guest authorization procedure
app.get("/guest", (req, res) => {
const base64Token = `${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`;
request(
{
url: "https://accounts.spotify.com/api/token",
method: "POST",
form: {
grant_type: "client_credentials"
},
json: true,
headers: {
Authorization: `Basic ${new Buffer(base64Token).toString("base64")}`
}
},
(err, response, body) => {
// res.send(body);
res.redirect(`${process.env.APP_URL}?${qs.stringify(body)}`);
}
);
});
//Refresh guest token
app.get("/guestrefresh", (req, res) => {
const base64Token = `${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`;
request(
{
url: "https://accounts.spotify.com/api/token",
method: "POST",
form: {
grant_type: "client_credentials"
},
headers: {
Authorization: `Basic ${new Buffer(base64Token).toString("base64")}`
}
},
(err, response, body) => {
res.send(body);
}
);
});
app.listen(process.env.PORT || "3400");