2024-07-08 11:44:39 -04:00
|
|
|
import { fail, redirect } from '@sveltejs/kit';
|
|
|
|
|
|
|
|
import type { Actions, PageServerLoad } from './$types';
|
2024-10-20 21:56:16 -04:00
|
|
|
import { getRandomBackground, getRandomQuote } from '$lib';
|
2024-11-29 14:41:13 -05:00
|
|
|
import { fetchCSRFToken } from '$lib/index.server';
|
2024-07-08 11:44:39 -04:00
|
|
|
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
|
|
|
|
|
|
|
|
export const load: PageServerLoad = async (event) => {
|
|
|
|
if (event.locals.user) {
|
|
|
|
return redirect(302, '/');
|
2024-10-20 21:56:16 -04:00
|
|
|
} else {
|
|
|
|
const quote = getRandomQuote();
|
|
|
|
const background = getRandomBackground();
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
quote,
|
|
|
|
background
|
|
|
|
}
|
|
|
|
};
|
2024-07-08 11:44:39 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const actions: Actions = {
|
|
|
|
default: async (event) => {
|
|
|
|
const formData = await event.request.formData();
|
|
|
|
const formUsername = formData.get('username');
|
|
|
|
|
|
|
|
let username = formUsername?.toString().toLocaleLowerCase();
|
|
|
|
|
|
|
|
const password = formData.get('password');
|
|
|
|
|
|
|
|
const serverEndpoint = PUBLIC_SERVER_URL || 'http://localhost:8000';
|
|
|
|
|
2024-11-29 14:41:13 -05:00
|
|
|
const csrfToken = await fetchCSRFToken();
|
2024-07-08 11:44:39 -04:00
|
|
|
|
2024-11-29 14:41:13 -05:00
|
|
|
const loginFetch = await event.fetch(`${serverEndpoint}/_allauth/browser/v1/auth/login`, {
|
2024-07-08 11:44:39 -04:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'X-CSRFToken': csrfToken,
|
2024-11-29 14:41:13 -05:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Cookie: `csrftoken=${csrfToken}`
|
2024-07-08 11:44:39 -04:00
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
username,
|
|
|
|
password
|
2024-11-29 14:41:13 -05:00
|
|
|
}),
|
|
|
|
credentials: 'include'
|
2024-07-08 11:44:39 -04:00
|
|
|
});
|
2024-11-29 14:41:13 -05:00
|
|
|
|
2024-07-08 11:44:39 -04:00
|
|
|
const loginResponse = await loginFetch.json();
|
|
|
|
if (!loginFetch.ok) {
|
|
|
|
// get the value of the first key in the object
|
|
|
|
const firstKey = Object.keys(loginResponse)[0] || 'error';
|
|
|
|
const error = loginResponse[firstKey][0] || 'Invalid username or password';
|
|
|
|
return fail(400, {
|
|
|
|
message: error
|
|
|
|
});
|
|
|
|
} else {
|
2024-11-29 14:41:13 -05:00
|
|
|
const setCookieHeader = loginFetch.headers.get('Set-Cookie');
|
2024-07-08 11:44:39 -04:00
|
|
|
|
2024-11-29 14:41:13 -05:00
|
|
|
console.log('setCookieHeader:', setCookieHeader);
|
|
|
|
|
|
|
|
if (setCookieHeader) {
|
|
|
|
// Regular expression to match sessionid cookie and its expiry
|
|
|
|
const sessionIdRegex = /sessionid=([^;]+).*?expires=([^;]+)/;
|
|
|
|
const match = setCookieHeader.match(sessionIdRegex);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
const sessionId = match[1];
|
|
|
|
const expiryString = match[2];
|
|
|
|
const expiryDate = new Date(expiryString);
|
|
|
|
|
|
|
|
console.log('Session ID:', sessionId);
|
|
|
|
console.log('Expiry Date:', expiryDate);
|
|
|
|
|
|
|
|
// Set the sessionid cookie
|
|
|
|
event.cookies.set('sessionid', sessionId, {
|
|
|
|
path: '/',
|
|
|
|
httpOnly: true,
|
|
|
|
sameSite: 'lax',
|
|
|
|
secure: true,
|
|
|
|
expires: expiryDate
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
redirect(302, '/');
|
2024-07-08 11:44:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|