message history + better auth

This commit is contained in:
2025-06-08 21:29:31 +02:00
parent e6b01b4828
commit 47de9dd35c
7 changed files with 91 additions and 20 deletions

View File

@@ -1,6 +1,32 @@
import { fail, type Actions } from '@sveltejs/kit';
import { error, fail, type Actions } from '@sveltejs/kit';
import { PUBLIC_BACKEND_URL } from '$env/static/public';
import { AUTH_COOIKIE_NAME } from '$lib/auth';
import type { PageServerLoad } from './$types';
import type { IMessage } from '$lib/message.svelte';
export const load: PageServerLoad = async ({ fetch, cookies }) => {
const authToken = cookies.get(AUTH_COOIKIE_NAME);
if (!authToken) {
error(401);
}
const response = await fetch(`${PUBLIC_BACKEND_URL}/messages`, {
headers: {
authorization: `Bearer ${authToken}`
}
});
if (!response.ok) {
error(response.status);
}
const messages = (await response.json()) as IMessage[];
return {
messages
};
};
export const actions = {
'send-message': async ({ request, fetch, cookies }) => {
@@ -12,7 +38,7 @@ export const actions = {
const formData = await request.formData();
const response = await fetch(`${PUBLIC_BACKEND_URL}/message`, {
await fetch(`${PUBLIC_BACKEND_URL}/message`, {
method: 'POST',
headers: {
authorization: `Bearer ${authToken}`