feat: add refresh token for spotify

This commit is contained in:
Zeynep Nur Temel
2023-03-19 23:04:55 +03:00
parent 56243aa076
commit ed87df212f
2 changed files with 32 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
import generateAuthUrl from './generate-auth-url';
import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified';
import refreshToken from './refresh-token';
export default {
fields: [
@@ -39,6 +40,7 @@ export default {
clickToCopy: false,
},
],
refreshToken,
generateAuthUrl,
verifyCredentials,
isStillVerified,

View File

@@ -0,0 +1,30 @@
import { IGlobalVariable } from '@automatisch/types';
import qs from 'qs';
const refreshToken = async ($: IGlobalVariable) => {
const stringifiedBody = qs.stringify({
refresh_token: $.auth.data.refreshToken as string,
grant_type: 'refresh_token',
});
const headers = {
Authorization: `Basic ${Buffer.from(
$.auth.data.clientId + ':' + $.auth.data.clientSecret
).toString('base64')}`,
'Content-Type': 'application/x-www-form-urlencoded',
};
const response = await $.http.post(
'https://accounts.spotify.com/api/token',
stringifiedBody,
{ headers }
);
await $.auth.set({
accessToken: response.data.access_token,
expiresIn: response.data.expires_in,
tokenType: response.data.token_type,
});
};
export default refreshToken;