Cleanup, fixing YT/Spotify auth

This commit is contained in:
2025-07-04 13:37:37 +02:00
parent dfd6d559bf
commit 270cd9ad05
11 changed files with 133 additions and 69 deletions

View File

@ -1,19 +1,21 @@
import { BASE_URL } from '$env/static/private';
import { Logger } from '$lib/log';
import { SpotifyPlaylistAdder } from '$lib/server/playlist/spotifyPlaylistAdder';
import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
const { DEV } = import.meta.env;
const logger = new Logger('SpotifyAuth');
export const load: PageServerLoad = async ({ url }) => {
const adder = new SpotifyPlaylistAdder();
let redirectUri = url;
if (url.hostname === 'localhost') {
redirectUri.hostname = '127.0.0.1';
let redirect_uri = new URL(`${BASE_URL}/spotifyAuth`);
if (url.hostname === 'localhost' && DEV) {
redirect_uri.hostname = '127.0.0.1';
}
logger.debug(url.searchParams, url.hostname);
if (url.searchParams.has('code')) {
await adder.receivedAuthCode(url.searchParams.get('code') || '', url);
await adder.receivedAuthCode(url.searchParams.get('code') || '', redirect_uri);
redirect(307, '/');
} else if (url.searchParams.has('error')) {
logger.error('received error', url.searchParams.get('error'));
@ -24,7 +26,7 @@ export const load: PageServerLoad = async ({ url }) => {
redirect(307, '/');
}
const authUrl = adder.constructAuthUrl(url);
const authUrl = adder.constructAuthUrl(redirect_uri);
logger.debug('+page.server.ts', authUrl.toString());
redirect(307, authUrl);
};