Merge pull request #203 from tejashah88/fix-unauthed-users-1

Fix: skip account setup when ALLOW_UNAUTHENTICATED is true
This commit is contained in:
Emrik Östling 2025-01-04 17:36:04 +01:00 committed by GitHub
commit f0fcfc159f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 64 additions and 40 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -3,10 +3,12 @@ import { Html } from "@kitajs/html";
export const Header = ({ export const Header = ({
loggedIn, loggedIn,
accountRegistration, accountRegistration,
allowUnauthenticated,
webroot = "", webroot = "",
}: { }: {
loggedIn?: boolean; loggedIn?: boolean;
accountRegistration?: boolean; accountRegistration?: boolean;
allowUnauthenticated?: boolean;
webroot?: string; webroot?: string;
}) => { }) => {
let rightNav: JSX.Element; let rightNav: JSX.Element;
@ -24,6 +26,7 @@ export const Header = ({
History History
</a> </a>
</li> </li>
{!allowUnauthenticated ? (
<li> <li>
<a <a
class={` class={`
@ -35,6 +38,7 @@ export const Header = ({
Logout Logout
</a> </a>
</li> </li>
) : null}
</ul> </ul>
); );
} else { } else {

View file

@ -215,6 +215,7 @@ const app = new Elysia({
<Header <Header
webroot={WEBROOT} webroot={WEBROOT}
accountRegistration={ACCOUNT_REGISTRATION} accountRegistration={ACCOUNT_REGISTRATION}
allowUnauthenticated={ALLOW_UNAUTHENTICATED}
/> />
<main class="w-full px-4"> <main class="w-full px-4">
<article class="article"> <article class="article">
@ -340,6 +341,7 @@ const app = new Elysia({
<Header <Header
webroot={WEBROOT} webroot={WEBROOT}
accountRegistration={ACCOUNT_REGISTRATION} accountRegistration={ACCOUNT_REGISTRATION}
allowUnauthenticated={ALLOW_UNAUTHENTICATED}
/> />
<main class="w-full px-4"> <main class="w-full px-4">
<article class="article"> <article class="article">
@ -457,36 +459,19 @@ const app = new Elysia({
return redirect(`${WEBROOT}/login`, 302); return redirect(`${WEBROOT}/login`, 302);
}) })
.get("/", async ({ jwt, redirect, cookie: { auth, jobId } }) => { .get("/", async ({ jwt, redirect, cookie: { auth, jobId } }) => {
if (!ALLOW_UNAUTHENTICATED) {
if (FIRST_RUN) { if (FIRST_RUN) {
return redirect(`${WEBROOT}/setup`, 302); return redirect(`${WEBROOT}/setup`, 302);
} }
if (!auth?.value && !ALLOW_UNAUTHENTICATED) { if (!auth?.value) {
return redirect(`${WEBROOT}/login`, 302); return redirect(`${WEBROOT}/login`, 302);
} }
}
// validate jwt // validate jwt
let user: ({ id: string } & JWTPayloadSpec) | false = false; let user: ({ id: string } & JWTPayloadSpec) | false = false;
if (auth?.value) { if (ALLOW_UNAUTHENTICATED) {
user = await jwt.verify(auth.value);
if (user !== false && user.id) {
if (Number.parseInt(user.id) < 2 ** 24 || !ALLOW_UNAUTHENTICATED) {
// make sure user exists in db
const existingUser = db
.query("SELECT * FROM users WHERE id = ?")
.as(User)
.get(user.id);
if (!existingUser) {
if (auth?.value) {
auth.remove();
}
return redirect(`${WEBROOT}/login`, 302);
}
}
}
} else if (ALLOW_UNAUTHENTICATED) {
const newUserId = String( const newUserId = String(
randomInt( randomInt(
2 ** 24, 2 ** 24,
@ -512,6 +497,25 @@ const app = new Elysia({
maxAge: 24 * 60 * 60, maxAge: 24 * 60 * 60,
sameSite: "strict", sameSite: "strict",
}); });
} else if (auth?.value) {
user = await jwt.verify(auth.value);
if (user !== false && user.id) {
if (Number.parseInt(user.id) < 2 ** 24 || !ALLOW_UNAUTHENTICATED) {
// make sure user exists in db
const existingUser = db
.query("SELECT * FROM users WHERE id = ?")
.as(User)
.get(user.id);
if (!existingUser) {
if (auth?.value) {
auth.remove();
}
return redirect(`${WEBROOT}/login`, 302);
}
}
}
} }
if (!user) { if (!user) {
@ -547,7 +551,11 @@ const app = new Elysia({
return ( return (
<BaseHtml webroot={WEBROOT}> <BaseHtml webroot={WEBROOT}>
<> <>
<Header webroot={WEBROOT} loggedIn /> <Header
webroot={WEBROOT}
allowUnauthenticated={ALLOW_UNAUTHENTICATED}
loggedIn
/>
<main class="w-full px-4"> <main class="w-full px-4">
<article class="article"> <article class="article">
<h1 class="mb-4 text-xl">Convert</h1> <h1 class="mb-4 text-xl">Convert</h1>
@ -951,7 +959,11 @@ const app = new Elysia({
return ( return (
<BaseHtml webroot={WEBROOT} title="ConvertX | Results"> <BaseHtml webroot={WEBROOT} title="ConvertX | Results">
<> <>
<Header webroot={WEBROOT} loggedIn /> <Header
webroot={WEBROOT}
allowUnauthenticated={ALLOW_UNAUTHENTICATED}
loggedIn
/>
<main class="w-full px-4"> <main class="w-full px-4">
<article class="article"> <article class="article">
<h1 class="mb-4 text-xl">Results</h1> <h1 class="mb-4 text-xl">Results</h1>
@ -1038,7 +1050,11 @@ const app = new Elysia({
return ( return (
<BaseHtml webroot={WEBROOT} title="ConvertX | Result"> <BaseHtml webroot={WEBROOT} title="ConvertX | Result">
<> <>
<Header webroot={WEBROOT} loggedIn /> <Header
webroot={WEBROOT}
allowUnauthenticated={ALLOW_UNAUTHENTICATED}
loggedIn
/>
<main class="w-full px-4"> <main class="w-full px-4">
<article class="article"> <article class="article">
<div class="mb-4 flex items-center justify-between"> <div class="mb-4 flex items-center justify-between">
@ -1284,7 +1300,11 @@ const app = new Elysia({
return ( return (
<BaseHtml webroot={WEBROOT} title="ConvertX | Converters"> <BaseHtml webroot={WEBROOT} title="ConvertX | Converters">
<> <>
<Header webroot={WEBROOT} loggedIn /> <Header
webroot={WEBROOT}
allowUnauthenticated={ALLOW_UNAUTHENTICATED}
loggedIn
/>
<main class="w-full px-4"> <main class="w-full px-4">
<article class="article"> <article class="article">
<h1 class="mb-4 text-xl">Converters</h1> <h1 class="mb-4 text-xl">Converters</h1>