terrible/app/state/session.js

33 lines
930 B
JavaScript
Raw Permalink Normal View History

2024-03-11 23:03:35 -05:00
import {redirect} from '@remix-run/react';
import namer from './namer';
export async function loadSession(request) {
const {parse} = await import('#state/cookie.server');
return parse(request, 'session');
}
export async function juggleSession(request) {
const session = await loadSession(request);
const url = new URL(request.url);
if (!session) {
if (!url.searchParams.has('session')) {
const [id] = crypto.getRandomValues(new Uint32Array(1));
throw redirect(`${url.origin}${url.pathname}?session`, {
headers: {
'Set-Cookie': await writeSession({id, name: namer()}),
},
});
}
}
else if (url.searchParams.has('session')) {
throw redirect(`${url.origin}${url.pathname}`);
}
return session ? session : {id: 0};
}
async function writeSession(session) {
const {serialize} = await import('#state/cookie.server');
return serialize('session', session);
}