This commit is contained in:
Alex
2025-03-01 12:40:28 +01:00
parent f00e0fa758
commit c6be9d2302

View File

@@ -1,14 +1,14 @@
import { BASE_API_URI } from "$lib/utils/constants";
import { formatError } from "$lib/utils/helpers";
import { fail, redirect } from "@sveltejs/kit";
import { BASE_API_URI } from '$lib/utils/constants';
import { formatError } from '$lib/utils/helpers';
import { fail, redirect } from '@sveltejs/kit';
/** @type {import('./$types').PageServerLoad} */
export async function load({ locals }) {
// redirect user if logged in
console.log("loading login page");
console.log('loading login page');
if (locals.user) {
console.log("user is logged in");
throw redirect(302, "/");
console.log('user is logged in');
throw redirect(302, '/');
}
}
@@ -22,26 +22,27 @@ export const actions = {
* @returns Error data or redirects user to the home page or the previous page
*/
login: async ({ request, fetch, cookies }) => {
console.log("login action called");
console.log('login action called');
const data = await request.formData();
const email = String(data.get("email"));
const password = String(data.get("password"));
const next = String(data.get("next"));
const email = String(data.get('email'));
const password = String(data.get('password'));
const next = String(data.get('next'));
/** @type {RequestInit} */
const requestInitOptions = {
method: "POST",
credentials: "include",
method: 'POST',
credentials: 'include',
headers: {
"Content-Type": "application/json",
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: email,
password: password,
}),
password: password
})
};
console.log('API call url:', `${BASE_API_URI}/users/login`);
const res = await fetch(`${BASE_API_URI}/users/login`, requestInitOptions);
console.log("Login response status:", res.status);
console.log("Login response headers:", Object.fromEntries(res.headers));
console.log('Login response status:', res.status);
console.log('Login response headers:', Object.fromEntries(res.headers));
if (!res.ok) {
const errorData = await res.json();
@@ -50,26 +51,26 @@ export const actions = {
}
const responseBody = await res.json();
console.log("Login response body:", responseBody);
console.log('Login response body:', responseBody);
// Extract the JWT from the response headers
const setCookieHeader = res.headers.get("set-cookie");
const setCookieHeader = res.headers.get('set-cookie');
if (setCookieHeader) {
const jwtMatch = setCookieHeader.match(/jwt=([^;]+)/);
if (jwtMatch) {
const jwtValue = jwtMatch[1];
// Set the cookie for the client
cookies.set("jwt", jwtValue, {
path: "/",
cookies.set('jwt', jwtValue, {
path: '/',
httpOnly: true,
secure: process.env.NODE_ENV === "production", // Secure in production
sameSite: "lax",
maxAge: 5 * 24 * 60 * 60, // 5 days in seconds
secure: process.env.NODE_ENV === 'production', // Secure in production
sameSite: 'lax',
maxAge: 5 * 24 * 60 * 60 // 5 days in seconds
});
}
}
console.log("Redirecting to:", next || "/");
throw redirect(303, next || "/");
},
console.log('Redirecting to:', next || '/');
throw redirect(303, next || '/');
}
};