forked from 409/chat-app-frontend
message history + better auth
This commit is contained in:
@@ -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}`
|
||||
|
||||
Reference in New Issue
Block a user