Skip to content

Commit

Permalink
fix: skip account setup when ALLOW_UNAUTHENTICATED is true
Browse files Browse the repository at this point in the history
  • Loading branch information
tejashah88 committed Jan 3, 2025
1 parent aa87bc5 commit 30fb4cb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 40 deletions.
Binary file modified bun.lockb
Binary file not shown.
26 changes: 15 additions & 11 deletions src/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { Html } from "@kitajs/html";
export const Header = ({
loggedIn,
accountRegistration,
allowUnauthenticated,
webroot = "",
}: {
loggedIn?: boolean;
accountRegistration?: boolean;
allowUnauthenticated?: boolean;
webroot?: string;
}) => {
let rightNav: JSX.Element;
Expand All @@ -24,17 +26,19 @@ export const Header = ({
History
</a>
</li>
<li>
<a
class={`
text-accent-600 transition-all
hover:text-accent-500 hover:underline
`}
href={`${webroot}/logoff`}
>
Logout
</a>
</li>
{!allowUnauthenticated ? (
<li>
<a
class={`
text-accent-600 transition-all
hover:text-accent-500 hover:underline
`}
href={`${webroot}/logoff`}
>
Logout
</a>
</li>
) : null}
</ul>
);
} else {
Expand Down
78 changes: 49 additions & 29 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ const app = new Elysia({
<Header
webroot={WEBROOT}
accountRegistration={ACCOUNT_REGISTRATION}
allowUnauthenticated={ALLOW_UNAUTHENTICATED}
/>
<main class="w-full px-4">
<article class="article">
Expand Down Expand Up @@ -340,6 +341,7 @@ const app = new Elysia({
<Header
webroot={WEBROOT}
accountRegistration={ACCOUNT_REGISTRATION}
allowUnauthenticated={ALLOW_UNAUTHENTICATED}
/>
<main class="w-full px-4">
<article class="article">
Expand Down Expand Up @@ -457,36 +459,19 @@ const app = new Elysia({
return redirect(`${WEBROOT}/login`, 302);
})
.get("/", async ({ jwt, redirect, cookie: { auth, jobId } }) => {
if (FIRST_RUN) {
return redirect(`${WEBROOT}/setup`, 302);
}
if (!ALLOW_UNAUTHENTICATED) {
if (FIRST_RUN) {
return redirect(`${WEBROOT}/setup`, 302);
}

if (!auth?.value && !ALLOW_UNAUTHENTICATED) {
return redirect(`${WEBROOT}/login`, 302);
if (!auth?.value) {
return redirect(`${WEBROOT}/login`, 302);
}
}

// validate jwt
let user: ({ id: string } & JWTPayloadSpec) | false = false;
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);
}
}
}
} else if (ALLOW_UNAUTHENTICATED) {
if (ALLOW_UNAUTHENTICATED) {
const newUserId = String(
randomInt(
2 ** 24,
Expand All @@ -512,6 +497,25 @@ const app = new Elysia({
maxAge: 24 * 60 * 60,
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) {
Expand Down Expand Up @@ -547,7 +551,11 @@ const app = new Elysia({
return (
<BaseHtml webroot={WEBROOT}>
<>
<Header webroot={WEBROOT} loggedIn />
<Header
webroot={WEBROOT}
allowUnauthenticated={ALLOW_UNAUTHENTICATED}
loggedIn
/>
<main class="w-full px-4">
<article class="article">
<h1 class="mb-4 text-xl">Convert</h1>
Expand Down Expand Up @@ -951,7 +959,11 @@ const app = new Elysia({
return (
<BaseHtml webroot={WEBROOT} title="ConvertX | Results">
<>
<Header webroot={WEBROOT} loggedIn />
<Header
webroot={WEBROOT}
allowUnauthenticated={ALLOW_UNAUTHENTICATED}
loggedIn
/>
<main class="w-full px-4">
<article class="article">
<h1 class="mb-4 text-xl">Results</h1>
Expand Down Expand Up @@ -1038,7 +1050,11 @@ const app = new Elysia({
return (
<BaseHtml webroot={WEBROOT} title="ConvertX | Result">
<>
<Header webroot={WEBROOT} loggedIn />
<Header
webroot={WEBROOT}
allowUnauthenticated={ALLOW_UNAUTHENTICATED}
loggedIn
/>
<main class="w-full px-4">
<article class="article">
<div class="mb-4 flex items-center justify-between">
Expand Down Expand Up @@ -1284,7 +1300,11 @@ const app = new Elysia({
return (
<BaseHtml webroot={WEBROOT} title="ConvertX | Converters">
<>
<Header webroot={WEBROOT} loggedIn />
<Header
webroot={WEBROOT}
allowUnauthenticated={ALLOW_UNAUTHENTICATED}
loggedIn
/>
<main class="w-full px-4">
<article class="article">
<h1 class="mb-4 text-xl">Converters</h1>
Expand Down

0 comments on commit 30fb4cb

Please sign in to comment.