diff --git a/src/lib/index.ts b/src/lib/index.ts index acad6b4..87429b1 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -21,9 +21,9 @@ export const scopeCookies = (cookies: Cookies, path: string) => { export const visitCountFile = `${env.WEBSITE_DATA_DIR}/visitcount` export const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0')); -const loginToBsky = () => { +export const loginToBsky = async () => { const creds = new CredentialSession(new URL("https://bsky.social")) - creds.login({identifier: 'gaze.systems', password: env.BSKY_PASSWORD ?? "" }) + await creds.login({identifier: 'gaze.systems', password: env.BSKY_PASSWORD ?? "" }) return new Agent(creds) } -export const bskyClient = writable(loginToBsky()) \ No newline at end of file +export const bskyClient = writable(null) diff --git a/src/routes/log/create/+server.ts b/src/routes/log/create/+server.ts index d269a82..55e64bb 100644 --- a/src/routes/log/create/+server.ts +++ b/src/routes/log/create/+server.ts @@ -1,6 +1,6 @@ import { env } from '$env/dynamic/private'; import { PUBLIC_BASE_URL } from '$env/static/public'; -import { bskyClient } from '$lib'; +import { bskyClient, loginToBsky } from '$lib'; import { createNote } from '$lib/notes.js'; import { get } from 'svelte/store'; @@ -20,7 +20,12 @@ export const POST = async ({ request }) => { const noteId = createNote({ content: noteData.content, published }) // bridge to bsky if want to bridge if (noteData.bskyPosse ?? null !== null) { - await get(bskyClient).post({text: `${noteData.content} (${PUBLIC_BASE_URL}/log?id=${noteId})`}) + let client = get(bskyClient) + if (client === null) { + client = await loginToBsky() + bskyClient.set(client) + } + await client.post({text: `${noteData.content} (${PUBLIC_BASE_URL}/log?id=${noteId})`}) } // send back created note id return new Response(JSON.stringify({ noteId }), { @@ -29,4 +34,4 @@ export const POST = async ({ request }) => { 'cache-control': 'no-store', } }) -}; \ No newline at end of file +};