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

@ -41,7 +41,7 @@ export class Logger {
public constructor(private name: string) {}
public static isDebugEnabled(): boolean {
return DEV;
return DEV || enableVerboseLog;
}
public verbose(...params: any[]) {
if (!enableVerboseLog) {
@ -50,7 +50,7 @@ export class Logger {
console.debug(new Date().toISOString(), `- ${this.name} -`, ...params);
}
public debug(...params: any[]) {
if (!Logger.isDebugEnabled()) {
if (false && !Logger.isDebugEnabled()) {
return;
}
console.debug(new Date().toISOString(), `- ${this.name} -`, ...params);

View File

@ -126,7 +126,7 @@ async function applyMigration(migration: Migration) {
`Fetching songs for existing post ${current.toString().padStart(4, '0')} of ${total}`,
post.url
);
const songs = await TimelineReader.getSongInfoInPost(post);
const songs = await TimelineReader.instance.getSongInfoInPost(post);
await saveSongInfoData(post.url, songs);
logger.debug(`Fetched ${songs.length} songs for existing post`, post.url);
}

View File

@ -14,9 +14,8 @@ export abstract class OauthPlaylistAdder {
public async authCodeExists(): Promise<boolean> {
try {
const fileHandle = await fs.open(this.token_file_name);
await fileHandle.close();
return true;
const token = await this.auth();
return token !== null && !token.error;
} catch {
this.logger.info('No auth token yet, authorizing...');
return false;
@ -45,7 +44,7 @@ export abstract class OauthPlaylistAdder {
tokenUrl: URL,
clientId: string,
code: string,
url: URL,
redirectUri: URL,
client_secret?: string,
customHeader?: HeadersInit
) {
@ -54,7 +53,7 @@ export abstract class OauthPlaylistAdder {
params.append('client_id', clientId);
params.append('code', code);
params.append('grant_type', 'authorization_code');
params.append('redirect_uri', `${url.origin}${url.pathname}`);
params.append('redirect_uri', redirectUri.toString());
if (client_secret) {
params.append('client_secret', client_secret);
}

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