File tree Expand file tree Collapse file tree 2 files changed +29
-2
lines changed
Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -13,8 +13,8 @@ function generateRandomString(length) {
1313function 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+ }
Original file line number Diff line number Diff line change @@ -4,5 +4,6 @@ const router = express.Router();
44
55//GET routes
66router . get ( '/' , spotify_controller . getAuthorization ) ;
7+ router . get ( '/callback' , spotify_controller . callBack ) ;
78
89module . exports = router ;
You can’t perform that action at this time.
0 commit comments