Skip to content

Commit

Permalink
Handle token refresh mechanic
Browse files Browse the repository at this point in the history
  • Loading branch information
danim1130 committed Jan 5, 2025
1 parent c22007f commit f54c370
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const spotifyApi = new SpotifyWebApi({
redirectUri: redirectUri,
});
let userEmail = "";
let refreshTokenIntervalId;

let actionId = 0;

exports.loadPackage = async function (gridController, persistedData) {
Expand All @@ -32,12 +34,13 @@ exports.loadPackage = async function (gridController, persistedData) {
);

if (persistedData) {
spotifyApi.setAccessToken(persistedData.accessToken);
spotifyApi.setRefreshToken(persistedData.refreshToken);
try {
await refreshSpotifyToken();
let me = await spotifyApi.getMe();
userEmail = me.body.email;
notifyPreference();
refreshTokenIntervalId = setInterval(refreshSpotifyToken, 1000 * 60 * 50);
} catch (e) {
console.error(e);
}
Expand Down Expand Up @@ -90,6 +93,7 @@ exports.loadPackage = async function (gridController, persistedData) {
};

exports.unloadPackage = async function () {
clearInterval(refreshTokenIntervalId);
while (--actionId >= 0) {
controller.sendMessageToEditor({
type: "remove-action",
Expand Down Expand Up @@ -192,6 +196,7 @@ async function onMessage(port, data) {
}
if (data.type === "logout-user") {
spotifyApi.resetCredentials();
clearInterval(refreshTokenIntervalId);
userEmail = "";
controller.sendMessageToEditor({
type: "persist-data",
Expand All @@ -210,6 +215,33 @@ function notifyPreference() {
});
}

async function refreshSpotifyToken() {
let refreshToken = spotifyApi.getRefreshToken();
const payload = {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
client_id: clientId,
grant_type: "refresh_token",
refresh_token: refreshToken,
}),
};
const body = await fetch("https://accounts.spotify.com/api/token", payload);
const response = await body.json();
spotifyApi.setAccessToken(response.access_token);
if (response.refresh_token) {
spotifyApi.setRefreshToken(response.refresh_token);
controller.sendMessageToEditor({
type: "persist-data",
data: {
refreshToken: response.refresh_token,
},
});
}
}

async function authorizeSpotify() {
let scope =
"user-read-playback-state user-modify-playback-state user-read-email user-library-read user-library-modify";
Expand Down Expand Up @@ -266,12 +298,16 @@ async function authorizeSpotify() {
controller.sendMessageToEditor({
type: "persist-data",
data: {
accessToken: response.access_token,
refreshToken: response.refresh_token,
},
});
let result = await spotifyApi.getMe();
userEmail = result.body.email;
clearInterval(refreshTokenIntervalId);
refreshTokenIntervalId = setInterval(
refreshSpotifyToken,
1000 * 60 * 50,
);
notifyPreference();
}
res.end(`Success!`);
Expand Down

0 comments on commit f54c370

Please sign in to comment.