You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.1 KiB
JavaScript

import { Client, Player } from "spotify-api.js";
import { store } from "../store.mjs";
import { UserStore } from "../db/schemas.mjs";
export const createLocalUser = async ({ refreshToken = undefined, code = undefined }) => {
const client = await Client.create({
refreshToken: true,
retryOnRateLimit: true,
token: {
clientID: store.clientID,
clientSecret: store.clientSecret,
redirectURL: store.redirectURL,
refreshToken,
code,
},
async onRefresh() {
await UserStore.findOneAndUpdate(
{ 'spotify.userId': client.user.id },
{ spotify: { refreshToken: client.refreshMeta.refreshToken } }
);
},
});
const player = new Player(client);
return { client, player };
};
export const findUserBySpotifyId = async (
{ userId, refreshToken },
{ create = false } = {},
) => {
const user = store.users.find(user => user.client.user.id === userId);
if (user) return user;
if (!create) return null;
const newUser = await createLocalUser({ refreshToken });
store.users.push(newUser);
return newUser;
}