Skip to content

Commit 3e85274

Browse files
committed
spotify callback function
1 parent bebb421 commit 3e85274

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

controllers/spotifyController.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ function generateRandomString(length) {
1313
function hashCode(code) {
1414
return crypto.createHash('sha256').update(code).digest('base64');
1515
}
16-
exports.getAuthorization = function (req, res) {
1716

17+
exports.getAuthorization = function (req, res) {
1818
let state = generateRandomString(16);
1919
let code_challenge = hashCode(generateRandomString(128));
2020
let scope = 'user-read-private playlist-read-private';
@@ -29,4 +29,30 @@ exports.getAuthorization = function (req, res) {
2929
code_challenge_method: 'S256',
3030
code_challenge: code_challenge
3131
}));
32-
};
32+
}
33+
34+
exports.callBack = function (req, res) {
35+
36+
let code = req.query.code || null;
37+
let state = req.query.state || null;
38+
39+
if (state === null) {
40+
res.redirect('/#' +
41+
new URLSearchParams({
42+
error: 'state_mismatch'
43+
}));
44+
} else {
45+
var authOptions = {
46+
url: 'https://accounts.spotify.com/api/token',
47+
form: {
48+
code: code,
49+
redirect_uri: redirect_uri,
50+
grant_type: 'authorization_code'
51+
},
52+
headers: {
53+
'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
54+
},
55+
json: true
56+
};
57+
}
58+
}

routes/spotify.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ const router = express.Router();
44

55
//GET routes
66
router.get('/', spotify_controller.getAuthorization);
7+
router.get('/callback', spotify_controller.callBack);
78

89
module.exports = router;

0 commit comments

Comments
 (0)