mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 06:04:05 +00:00
editorconfig
This commit is contained in:
@@ -13,125 +13,125 @@ const logger = require("../utils/logger")(module);
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
const login = (_req, res) => {
|
||||
try {
|
||||
const state = generateRandString(16);
|
||||
res.cookie(stateKey, state);
|
||||
try {
|
||||
const state = generateRandString(16);
|
||||
res.cookie(stateKey, state);
|
||||
|
||||
const scope = Object.values(scopes).join(" ");
|
||||
res.redirect(
|
||||
`${accountsAPIURL}/authorize?` +
|
||||
new URLSearchParams({
|
||||
response_type: "code",
|
||||
client_id: process.env.CLIENT_ID,
|
||||
scope: scope,
|
||||
redirect_uri: process.env.REDIRECT_URI,
|
||||
state: state
|
||||
}).toString()
|
||||
);
|
||||
return;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("login", { error });
|
||||
return;
|
||||
}
|
||||
const scope = Object.values(scopes).join(" ");
|
||||
res.redirect(
|
||||
`${accountsAPIURL}/authorize?` +
|
||||
new URLSearchParams({
|
||||
response_type: "code",
|
||||
client_id: process.env.CLIENT_ID,
|
||||
scope: scope,
|
||||
redirect_uri: process.env.REDIRECT_URI,
|
||||
state: state
|
||||
}).toString()
|
||||
);
|
||||
return;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("login", { error });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exchange authorization code for refresh and access tokens
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
const callback = async (req, res) => {
|
||||
try {
|
||||
const { code, state, error } = req.query;
|
||||
const storedState = req.cookies ? req.cookies[stateKey] : null;
|
||||
try {
|
||||
const { code, state, error } = req.query;
|
||||
const storedState = req.cookies ? req.cookies[stateKey] : null;
|
||||
|
||||
// check state
|
||||
if (state === null || state !== storedState) {
|
||||
res.redirect(409, "/");
|
||||
logger.warn("state mismatch");
|
||||
return;
|
||||
} else if (error) {
|
||||
res.status(401).send({ message: "Auth callback error" });
|
||||
logger.error("callback error", { error });
|
||||
return;
|
||||
} else {
|
||||
// get auth tokens
|
||||
res.clearCookie(stateKey);
|
||||
// check state
|
||||
if (state === null || state !== storedState) {
|
||||
res.redirect(409, "/");
|
||||
logger.warn("state mismatch");
|
||||
return;
|
||||
} else if (error) {
|
||||
res.status(401).send({ message: "Auth callback error" });
|
||||
logger.error("callback error", { error });
|
||||
return;
|
||||
} else {
|
||||
// get auth tokens
|
||||
res.clearCookie(stateKey);
|
||||
|
||||
const authForm = {
|
||||
code: code,
|
||||
redirect_uri: process.env.REDIRECT_URI,
|
||||
grant_type: "authorization_code"
|
||||
}
|
||||
const authForm = {
|
||||
code: code,
|
||||
redirect_uri: process.env.REDIRECT_URI,
|
||||
grant_type: "authorization_code"
|
||||
}
|
||||
|
||||
const authPayload = (new URLSearchParams(authForm)).toString();
|
||||
const authPayload = (new URLSearchParams(authForm)).toString();
|
||||
|
||||
const tokenResponse = await authInstance.post("/api/token", authPayload);
|
||||
const tokenResponse = await authInstance.post("/api/token", authPayload);
|
||||
|
||||
if (tokenResponse.status === 200) {
|
||||
logger.debug("Tokens obtained.");
|
||||
req.session.accessToken = tokenResponse.data.access_token;
|
||||
req.session.refreshToken = tokenResponse.data.refresh_token;
|
||||
} else {
|
||||
logger.error("login failed", { statusCode: tokenResponse.status });
|
||||
res.status(tokenResponse.status).send({ message: "Error: Login failed" });
|
||||
}
|
||||
if (tokenResponse.status === 200) {
|
||||
logger.debug("Tokens obtained.");
|
||||
req.session.accessToken = tokenResponse.data.access_token;
|
||||
req.session.refreshToken = tokenResponse.data.refresh_token;
|
||||
} else {
|
||||
logger.error("login failed", { statusCode: tokenResponse.status });
|
||||
res.status(tokenResponse.status).send({ message: "Error: Login failed" });
|
||||
}
|
||||
|
||||
const userData = await getUserProfile(req, res);
|
||||
if (res.headersSent) return;
|
||||
const userData = await getUserProfile(req, res);
|
||||
if (res.headersSent) return;
|
||||
|
||||
/** @type {typedefs.User} */
|
||||
req.session.user = {
|
||||
username: userData.display_name,
|
||||
id: userData.id,
|
||||
};
|
||||
/** @type {typedefs.User} */
|
||||
req.session.user = {
|
||||
username: userData.display_name,
|
||||
id: userData.id,
|
||||
};
|
||||
|
||||
// res.status(200).send({ message: "OK" });
|
||||
res.redirect(process.env.APP_URI + "?login=success");
|
||||
logger.debug("New login.", { username: userData.display_name });
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("callback", { error });
|
||||
return;
|
||||
}
|
||||
// res.status(200).send({ message: "OK" });
|
||||
res.redirect(process.env.APP_URI + "?login=success");
|
||||
logger.debug("New login.", { username: userData.display_name });
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("callback", { error });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Request new access token using refresh token
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
const refresh = async (req, res) => {
|
||||
try {
|
||||
const authForm = {
|
||||
refresh_token: req.session.refreshToken,
|
||||
grant_type: "refresh_token",
|
||||
}
|
||||
try {
|
||||
const authForm = {
|
||||
refresh_token: req.session.refreshToken,
|
||||
grant_type: "refresh_token",
|
||||
}
|
||||
|
||||
const authPayload = (new URLSearchParams(authForm)).toString();
|
||||
const authPayload = (new URLSearchParams(authForm)).toString();
|
||||
|
||||
const response = await authInstance.post("/api/token", authPayload);
|
||||
const response = await authInstance.post("/api/token", authPayload);
|
||||
|
||||
if (response.status === 200) {
|
||||
req.session.accessToken = response.data.access_token;
|
||||
req.session.refreshToken = response.data.refresh_token ?? req.session.refreshToken; // refresh token rotation
|
||||
if (response.status === 200) {
|
||||
req.session.accessToken = response.data.access_token;
|
||||
req.session.refreshToken = response.data.refresh_token ?? req.session.refreshToken; // refresh token rotation
|
||||
|
||||
res.status(200).send({ message: "OK" });
|
||||
logger.debug(`Access token refreshed${(response.data.refresh_token !== null) ? " and refresh token updated" : ""}.`);
|
||||
return;
|
||||
} else {
|
||||
res.status(response.status).send({ message: "Error: Refresh token flow failed." });
|
||||
logger.error("refresh failed", { statusCode: response.status });
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("refresh", { error });
|
||||
return;
|
||||
}
|
||||
res.status(200).send({ message: "OK" });
|
||||
logger.debug(`Access token refreshed${(response.data.refresh_token !== null) ? " and refresh token updated" : ""}.`);
|
||||
return;
|
||||
} else {
|
||||
res.status(response.status).send({ message: "Error: Refresh token flow failed." });
|
||||
logger.error("refresh failed", { statusCode: response.status });
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("refresh", { error });
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -140,30 +140,30 @@ const refresh = async (req, res) => {
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
const logout = async (req, res) => {
|
||||
try {
|
||||
const delSession = req.session.destroy((error) => {
|
||||
if (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("Error while logging out", { error });
|
||||
return;
|
||||
} else {
|
||||
res.clearCookie(sessionName);
|
||||
// res.status(200).send({ message: "OK" });
|
||||
res.redirect(process.env.APP_URI + "?logout=success");
|
||||
logger.debug("Logged out.", { sessionID: delSession.id });
|
||||
return;
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("logout", { error });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const delSession = req.session.destroy((error) => {
|
||||
if (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("Error while logging out", { error });
|
||||
return;
|
||||
} else {
|
||||
res.clearCookie(sessionName);
|
||||
// res.status(200).send({ message: "OK" });
|
||||
res.redirect(process.env.APP_URI + "?logout=success");
|
||||
logger.debug("Logged out.", { sessionID: delSession.id });
|
||||
return;
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("logout", { error });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
login,
|
||||
callback,
|
||||
refresh,
|
||||
logout
|
||||
login,
|
||||
callback,
|
||||
refresh,
|
||||
logout
|
||||
};
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -10,150 +10,150 @@ const { parseSpotifyLink } = require("../utils/spotifyURITransformer");
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
const fetchUserPlaylists = async (req, res) => {
|
||||
try {
|
||||
let userPlaylists = {};
|
||||
try {
|
||||
let userPlaylists = {};
|
||||
|
||||
// get first 50
|
||||
const respData = await getUserPlaylistsFirstPage(req, res);
|
||||
if (res.headersSent) return;
|
||||
// get first 50
|
||||
const respData = await getUserPlaylistsFirstPage(req, res);
|
||||
if (res.headersSent) return;
|
||||
|
||||
userPlaylists.total = respData.total;
|
||||
userPlaylists.total = respData.total;
|
||||
|
||||
userPlaylists.items = respData.items.map((playlist) => {
|
||||
return {
|
||||
uri: playlist.uri,
|
||||
images: playlist.images,
|
||||
name: playlist.name,
|
||||
total: playlist.tracks.total
|
||||
}
|
||||
});
|
||||
userPlaylists.items = respData.items.map((playlist) => {
|
||||
return {
|
||||
uri: playlist.uri,
|
||||
images: playlist.images,
|
||||
name: playlist.name,
|
||||
total: playlist.tracks.total
|
||||
}
|
||||
});
|
||||
|
||||
userPlaylists.next = respData.next;
|
||||
// keep getting batches of 50 till exhausted
|
||||
while (userPlaylists.next) {
|
||||
const nextData = await getUserPlaylistsNextPage(req, res, userPlaylists.next);
|
||||
if (res.headersSent) return;
|
||||
userPlaylists.next = respData.next;
|
||||
// keep getting batches of 50 till exhausted
|
||||
while (userPlaylists.next) {
|
||||
const nextData = await getUserPlaylistsNextPage(req, res, userPlaylists.next);
|
||||
if (res.headersSent) return;
|
||||
|
||||
userPlaylists.items.push(
|
||||
...nextData.items.map((playlist) => {
|
||||
return {
|
||||
uri: playlist.uri,
|
||||
images: playlist.images,
|
||||
name: playlist.name,
|
||||
total: playlist.tracks.total
|
||||
}
|
||||
})
|
||||
);
|
||||
userPlaylists.items.push(
|
||||
...nextData.items.map((playlist) => {
|
||||
return {
|
||||
uri: playlist.uri,
|
||||
images: playlist.images,
|
||||
name: playlist.name,
|
||||
total: playlist.tracks.total
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
userPlaylists.next = nextData.next;
|
||||
}
|
||||
userPlaylists.next = nextData.next;
|
||||
}
|
||||
|
||||
delete userPlaylists.next;
|
||||
delete userPlaylists.next;
|
||||
|
||||
res.status(200).send(userPlaylists);
|
||||
logger.debug("Fetched user playlists", { num: userPlaylists.total });
|
||||
return;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("fetchUserPlaylists", { error });
|
||||
return;
|
||||
}
|
||||
res.status(200).send(userPlaylists);
|
||||
logger.debug("Fetched user playlists", { num: userPlaylists.total });
|
||||
return;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("fetchUserPlaylists", { error });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an entire playlist
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
const fetchPlaylistDetails = async (req, res) => {
|
||||
try {
|
||||
let playlist = {};
|
||||
/** @type {typedefs.URIObject} */
|
||||
let uri;
|
||||
let initialFields = ["collaborative", "description", "images", "name", "owner(uri,display_name)", "public",
|
||||
"snapshot_id", "tracks(next,total,items(is_local,track(name,uri)))"];
|
||||
let mainFields = ["next,items(is_local,track(name,uri))"];
|
||||
try {
|
||||
let playlist = {};
|
||||
/** @type {typedefs.URIObject} */
|
||||
let uri;
|
||||
let initialFields = ["collaborative", "description", "images", "name", "owner(uri,display_name)", "public",
|
||||
"snapshot_id", "tracks(next,total,items(is_local,track(name,uri)))"];
|
||||
let mainFields = ["next,items(is_local,track(name,uri))"];
|
||||
|
||||
try {
|
||||
uri = parseSpotifyLink(req.query.playlist_link)
|
||||
if (uri.type !== "playlist") {
|
||||
res.status(400).send({ message: "Link is not a playlist" });
|
||||
logger.warn("non-playlist link provided", { uri });
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(400).send({ message: error.message });
|
||||
logger.warn("parseSpotifyLink", { error });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
uri = parseSpotifyLink(req.query.playlist_link)
|
||||
if (uri.type !== "playlist") {
|
||||
res.status(400).send({ message: "Link is not a playlist" });
|
||||
logger.warn("non-playlist link provided", { uri });
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(400).send({ message: error.message });
|
||||
logger.warn("parseSpotifyLink", { error });
|
||||
return;
|
||||
}
|
||||
|
||||
const respData = await getPlaylistDetailsFirstPage(req, res, initialFields.join(), uri.id);
|
||||
if (res.headersSent) return;
|
||||
const respData = await getPlaylistDetailsFirstPage(req, res, initialFields.join(), uri.id);
|
||||
if (res.headersSent) return;
|
||||
|
||||
// TODO: this whole section needs to be DRYer
|
||||
// look into serializr
|
||||
playlist.name = respData.name;
|
||||
playlist.description = respData.description;
|
||||
playlist.collaborative = respData.collaborative;
|
||||
playlist.public = respData.public;
|
||||
playlist.images = [...respData.images];
|
||||
playlist.owner = { ...respData.owner };
|
||||
playlist.snapshot_id = respData.snapshot_id;
|
||||
playlist.total = respData.tracks.total;
|
||||
// TODO: this whole section needs to be DRYer
|
||||
// look into serializr
|
||||
playlist.name = respData.name;
|
||||
playlist.description = respData.description;
|
||||
playlist.collaborative = respData.collaborative;
|
||||
playlist.public = respData.public;
|
||||
playlist.images = [...respData.images];
|
||||
playlist.owner = { ...respData.owner };
|
||||
playlist.snapshot_id = respData.snapshot_id;
|
||||
playlist.total = respData.tracks.total;
|
||||
|
||||
// previous fields get carried over to the next URL, but most of these fields are not present in the new endpoint
|
||||
// API shouldn't be returning such URLs, the problem's in the API ig...
|
||||
if (respData.tracks.next) {
|
||||
playlist.next = new URL(respData.tracks.next);
|
||||
playlist.next.searchParams.set("fields", mainFields.join());
|
||||
playlist.next = playlist.next.href;
|
||||
}
|
||||
playlist.tracks = respData.tracks.items.map((playlist_item) => {
|
||||
return {
|
||||
is_local: playlist_item.is_local,
|
||||
track: {
|
||||
name: playlist_item.track.name,
|
||||
type: playlist_item.track.type,
|
||||
uri: playlist_item.track.uri
|
||||
}
|
||||
}
|
||||
});
|
||||
// previous fields get carried over to the next URL, but most of these fields are not present in the new endpoint
|
||||
// API shouldn't be returning such URLs, the problem's in the API ig...
|
||||
if (respData.tracks.next) {
|
||||
playlist.next = new URL(respData.tracks.next);
|
||||
playlist.next.searchParams.set("fields", mainFields.join());
|
||||
playlist.next = playlist.next.href;
|
||||
}
|
||||
playlist.tracks = respData.tracks.items.map((playlist_item) => {
|
||||
return {
|
||||
is_local: playlist_item.is_local,
|
||||
track: {
|
||||
name: playlist_item.track.name,
|
||||
type: playlist_item.track.type,
|
||||
uri: playlist_item.track.uri
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// keep getting batches of 50 till exhausted
|
||||
while (playlist.next) {
|
||||
const nextData = await getPlaylistDetailsNextPage(req, res, playlist.next);
|
||||
if (res.headersSent) return;
|
||||
// keep getting batches of 50 till exhausted
|
||||
while (playlist.next) {
|
||||
const nextData = await getPlaylistDetailsNextPage(req, res, playlist.next);
|
||||
if (res.headersSent) return;
|
||||
|
||||
playlist.tracks.push(
|
||||
...nextData.items.map((playlist_item) => {
|
||||
return {
|
||||
is_local: playlist_item.is_local,
|
||||
track: {
|
||||
name: playlist_item.track.name,
|
||||
type: playlist_item.track.type,
|
||||
uri: playlist_item.track.uri
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
playlist.tracks.push(
|
||||
...nextData.items.map((playlist_item) => {
|
||||
return {
|
||||
is_local: playlist_item.is_local,
|
||||
track: {
|
||||
name: playlist_item.track.name,
|
||||
type: playlist_item.track.type,
|
||||
uri: playlist_item.track.uri
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
playlist.next = nextData.next;
|
||||
}
|
||||
playlist.next = nextData.next;
|
||||
}
|
||||
|
||||
delete playlist.next;
|
||||
delete playlist.next;
|
||||
|
||||
res.status(200).send(playlist);
|
||||
logger.debug("Fetched playlist tracks", { num: playlist.tracks.length });
|
||||
return;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("getPlaylistDetails", { error });
|
||||
return;
|
||||
}
|
||||
res.status(200).send(playlist);
|
||||
logger.debug("Fetched playlist tracks", { num: playlist.tracks.length });
|
||||
return;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("getPlaylistDetails", { error });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetchUserPlaylists,
|
||||
fetchPlaylistDetails
|
||||
fetchUserPlaylists,
|
||||
fetchPlaylistDetails
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user