lunify.js
Version:
A basic api wrapper for the spotify api covering the oauth routes.
95 lines • 3.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OauthManager = void 0;
const node_events_1 = require("node:events");
const __1 = require("../..");
const user_1 = require("../../structures/user");
/* eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging */
class OauthManager extends node_events_1.EventEmitter {
client;
constructor(client) {
super();
this.client = client;
}
/**
* Create a oAuth url for users to authorize
* @param {Scopes[]} scopes - A list of spotify scopes {@link https://developer.spotify.com/documentation/web-api/concepts/scopes}
* @param {?string} state - If you want to use your own state use this
*/
generateUrl(scopes, state) {
const redirectUri = this.client.options.oauth?.redirectUri;
if (!redirectUri)
throw new Error(__1.LunifyErrors.NoRedirectUri);
const params = new URLSearchParams();
params.append("response_type", "code");
params.append("client_id", this.client.options.clientId);
params.append("redirect_uri", redirectUri);
params.append("scope", scopes.join(" "));
params.append("state", state || crypto.randomUUID());
return "https://accounts.spotify.com/authorize?" + params.toString();
}
/**
* Get a spotify access token from a oAuth code
* @param {string} code - oauth response query code
* @example ```ts
* const code = req.query.code;
* const access = await api.oauth.fetchToken(code);
* ```
*/
async fetchToken(code) {
const redirectUri = this.client.options.oauth?.redirectUri;
if (!redirectUri)
throw new Error(__1.LunifyErrors.NoRedirectUri);
const params = new URLSearchParams();
params.append("grant_type", "authorization_code");
params.append("redirect_uri", redirectUri);
params.append("code", code);
const res = await this.client.rest.post("/token", {
domain: __1.RequestDomain.Accounts,
authRequired: true,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: params
});
res.created_timestamp = Date.now();
return new user_1.UserOauth(this.client, res);
}
/**
* Refresh a spotify access token
* @param {string} refreshToken - oauth refresh token
* @example ```ts
* const refreshToken = ...;
* await api.oauth.refreshToken(refreshToken);
* ```
*/
async refreshToken(refreshToken) {
const params = new URLSearchParams();
params.append("grant_type", "refresh_token");
params.append("refresh_token", refreshToken);
const res = await this.client.rest.post("/token", {
domain: __1.RequestDomain.Accounts,
authRequired: true,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: params
});
if ("message" in res) {
throw new Error(res.message);
}
// Sometimes, <UserOauth>.refreshToken is null for some reason.
// Spotify claims they return a new refresh token, but I don't trust them.
// I am just testing things and see what sticks, will be removed if this doesn't fix it.
// It only happens sometimes, it seems to only appear after a few days without restarting the node process.
// Contact me: luna@waya.one - https://discord.gg/yYd6YKHQZH
// https://developer.spotify.com/documentation/web-api/tutorials/refreshing-tokens
res.refresh_token ||= refreshToken;
res.created_timestamp = Date.now();
const oauth = new user_1.UserOauth(this.client, res);
this.emit("refresh", oauth);
return oauth;
}
}
exports.OauthManager = OauthManager;
//# sourceMappingURL=index.js.map