Skip to content

Instantly share code, notes, and snippets.

@cdaz5
Created May 28, 2018 17:32
Show Gist options
  • Select an option

  • Save cdaz5/8186b0d03d9e2051846428b215bb7d44 to your computer and use it in GitHub Desktop.

Select an option

Save cdaz5/8186b0d03d9e2051846428b215bb7d44 to your computer and use it in GitHub Desktop.
const clientID = '82cf58344ff048c9b6df55e2b480e977';
let accessToken; // we use let because we plan to reassign it later
const redirectUri = 'http://localhost:3000/';
const Spotify = {
get getAccessToken() {
if (accessToken) {
return accessToken; // we return the token if it exists your naming was off is all
}
const accessTokenMatch = window.location.href.match(/access_token=([^&]*)/);
const expiresInMatch = window.location.href.match(/expires_in([^&]*)/);
if (accessTokenMatch && expiresInMatch) {
accessToken = accessTokenMatch[1];
const expiresIn = Number(expiresInMatch[1]);
window.setTimeout(() => accessToken = '', expiresIn * 1000);
window.history.pushState('Access Token', null, '/');
return accessToken;
} else {
const accessUrl = `https://accounts.spotify.com/authorize?client_id=${clientId}&response_type=token&scope=playlist-modify-public&redirect_uri=${redirectUri}`;
window.location = accessUrl;
}
}
};
export default Spotify;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment