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

@ -100,7 +100,7 @@
});
}
onMount(async () => {
onMount(() => {
posts = data.posts;
if (posts.length > 0) {
oldestBeforeLastFetch = new Date(posts[posts.length - 1].created_at).getTime();

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);
};

View File

@ -1,3 +1,4 @@
import { BASE_URL } from '$env/static/private';
import { Logger } from '$lib/log';
import { YoutubePlaylistAdder } from '$lib/server/playlist/ytPlaylistAdder';
import { redirect } from '@sveltejs/kit';
@ -7,9 +8,10 @@ const logger = new Logger('YT Auth');
export const load: PageServerLoad = async ({ url }) => {
const adder = new YoutubePlaylistAdder();
const redirect_uri = new URL(`${BASE_URL}/ytauth`);
if (url.searchParams.has('code')) {
logger.debug(url.searchParams);
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'));
@ -19,8 +21,7 @@ export const load: PageServerLoad = async ({ url }) => {
if (await adder.authCodeExists()) {
redirect(307, '/');
}
const authUrl = adder.constructAuthUrl(url);
const authUrl = adder.constructAuthUrl(redirect_uri);
logger.debug('+page.server.ts', authUrl.toString());
redirect(307, authUrl);
};