Skip to content

Instantly share code, notes, and snippets.

@MatthewStanciu
Created September 8, 2018 02:18
Show Gist options
  • Save MatthewStanciu/b555a6c42b5e402442afe75164980463 to your computer and use it in GitHub Desktop.
Save MatthewStanciu/b555a6c42b5e402442afe75164980463 to your computer and use it in GitHub Desktop.
var express = require('express');
var app = express();
var http = require('http').Server(app);
var bodyParser = require('body-parser');
var request = require('request');
var querystring = require('querystring');
var accessToken = '';
var refreshToken = '';
var trackUri = '';
var stateKey = 'spotify_auth_state';
var redirect_uri = 'http://localhost:3000/callback'
var generateRandomString = function(length) {
var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
app.get('/login', function(req, res) {
var state = generateRandomString(16);
res.cookie(stateKey, state);
var scope = 'user-read-private user-read-email playlist-modify-private playlist-modify-public';
res.redirect('https://accounts.spotify.com/authorize?' +
querystring.stringify({
response_type: 'code',
client_id: 'YOUR-CLIENT-ID',
scope: scope,
redirect_uri: redirect_uri,
state: state
}));
});
app.get('/callback', function(req, res) {
var code = req.query.code || null;
var state = req.query.state || null;
var storedState = req.cookies ? req.cookies[stateKey] : null;
res.send(code);
});
app.get('/', function(req, res) {
return res.redirect('/login');
})
http.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment