1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-27 16:59:37 +02:00
AdventureLog/frontend/src/routes/login/+page.server.ts

124 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-12-12 15:45:19 -05:00
import { fail, redirect, type RequestEvent } from '@sveltejs/kit';
2024-07-08 11:44:39 -04:00
2024-12-12 15:45:19 -05:00
import type { Actions, PageServerLoad, RouteParams } from './$types';
2024-10-20 21:56:16 -04:00
import { getRandomBackground, getRandomQuote } from '$lib';
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');
2024-12-12 15:45:19 -05:00
const username = formUsername?.toString().toLowerCase();
2024-07-08 11:44:39 -04:00
const password = formData.get('password');
2024-12-12 15:45:19 -05:00
const totp = formData.get('totp');
2024-07-08 11:44:39 -04:00
const serverEndpoint = PUBLIC_SERVER_URL || 'http://localhost:8000';
const csrfToken = await fetchCSRFToken();
2024-07-08 11:44:39 -04:00
2024-12-12 15:45:19 -05:00
// Initial login attempt
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,
'Content-Type': 'application/json',
Cookie: `csrftoken=${csrfToken}`
2024-07-08 11:44:39 -04:00
},
2024-12-12 15:45:19 -05:00
body: JSON.stringify({ username, password }),
credentials: 'include'
2024-07-08 11:44:39 -04:00
});
2024-12-12 15:45:19 -05:00
if (loginFetch.status === 200) {
// Login successful without MFA
handleSuccessfulLogin(event, loginFetch);
return redirect(302, '/');
} else if (loginFetch.status === 401) {
// MFA required
if (!totp) {
return fail(401, {
message: 'Multi-factor authentication required',
mfa_required: true
});
} else {
// Attempt MFA authentication
const sessionId = extractSessionId(loginFetch.headers.get('Set-Cookie'));
const mfaLoginFetch = await event.fetch(
`${serverEndpoint}/_allauth/browser/v1/auth/2fa/authenticate`,
{
method: 'POST',
headers: {
'X-CSRFToken': csrfToken,
'Content-Type': 'application/json',
Cookie: `csrftoken=${csrfToken}; sessionid=${sessionId}`
},
body: JSON.stringify({ code: totp }),
credentials: 'include'
}
);
2024-12-12 15:45:19 -05:00
if (mfaLoginFetch.ok) {
// MFA successful
handleSuccessfulLogin(event, mfaLoginFetch);
return redirect(302, '/');
} else {
// MFA failed
const mfaLoginResponse = await mfaLoginFetch.json();
return fail(401, {
message: mfaLoginResponse.error || 'Invalid MFA code',
mfa_required: true
});
}
}
2024-12-12 15:45:19 -05:00
} else {
// Login failed
const loginResponse = await loginFetch.json();
const firstKey = Object.keys(loginResponse)[0] || 'error';
const error = loginResponse[firstKey][0] || 'Invalid username or password';
return fail(400, { message: error });
2024-07-08 11:44:39 -04:00
}
}
};
2024-12-12 15:45:19 -05:00
function handleSuccessfulLogin(event: RequestEvent<RouteParams, '/login'>, response: Response) {
const setCookieHeader = response.headers.get('Set-Cookie');
if (setCookieHeader) {
const sessionIdRegex = /sessionid=([^;]+).*?expires=([^;]+)/;
const match = setCookieHeader.match(sessionIdRegex);
if (match) {
const [, sessionId, expiryString] = match;
event.cookies.set('sessionid', sessionId, {
path: '/',
httpOnly: true,
sameSite: 'lax',
secure: true,
expires: new Date(expiryString)
});
}
}
}
function extractSessionId(setCookieHeader: string | null) {
if (setCookieHeader) {
const sessionIdRegex = /sessionid=([^;]+)/;
const match = setCookieHeader.match(sessionIdRegex);
return match ? match[1] : '';
}
return '';
}