From 538c5b60c9e27a8184740305475245da79bae143 Mon Sep 17 00:00:00 2001 From: Tejas Shah Date: Fri, 3 Jan 2025 15:06:38 -0800 Subject: [PATCH] fix: skip account setup when ALLOW_UNAUTHENTICATED is true --- bun.lockb | Bin 168320 -> 168320 bytes src/components/header.tsx | 26 +++++++------ src/index.tsx | 78 ++++++++++++++++++++++++-------------- 3 files changed, 64 insertions(+), 40 deletions(-) diff --git a/bun.lockb b/bun.lockb index 61bc091d3b8b4fab76925b40392fd7985a81361f..68fabb93cbcf9097d37a28c2e8c33959dba271ba 100755 GIT binary patch delta 1386 zcmXYw%TH556vpSYQ~@c9P(YAJsq&_2p|%u?ELf-*jH1Lx6e$mbiZPgo_=v5bA^1p) z37%jK!9opSTxrrkiHYt_bm2c>j5~KOG;!f~+B3Od=9}M~nLGEKxzVNUD4zH9XCabf z((Nc2gU48)?aGcRvtpd5tXEkE#ygcAS7yUFU)c#|TQDwC)(0l*wnDqoEMiV;U?xCm z8k#d;a*!;jLfKi3%f`4vPK0(&**18;vVLXqVa>|UE3<=nl?|j=#F$*bQAk=?10@4v zDPqhZm>gyYbXD1~#z|Htl#M8ptk`7%+67%JS@{7kUq7mG1@Ipu7)ZOQ&;j4A>=Js} zrx21-NGiuPPEtAoNxPzP#qh%^f`fv|hf11%67wd zYr1YID+BX^NqQ%hmBY7a+)ZUtwl|Pu=$5id_$kfI?KF!RC*ZWgJAkrJ71RJPsgw-i zF)nCP3|ds#9{5+v?kU>~_5xlqIi;)`{*=Z|E2{y^CDdP1d0$~Ie2xY_KriR2gRyj|mF(Ti<>3Sp!S50y2-cfd;-9w}>r4?|Lh$2=Y*7nSo|jA|T}AWt=_S!VHi zCx4AmzQc*(GoA9m&!(pS#;CnRKJK|rwZaGBO%f?SX@@#{4mAX^5GY31MwRb<8S=0#|M!z(moX)^q2g?`%PijVQz zRqEn@t5ln{53A+2R&w1M4cWr5L4LAECoO{}@qLYYa>>V@btJPX*a|4zFE=*F; m>ciUJBsL51Op?6TR=1avNs5x6Lz`5BF7E>sBrb1~Eg)E+AfO;k=@%9uByuo9G>Re|1c9OwK?#Xb#E(|+3-w@3 zWP&k73nhT@q)AgsOicI#^x(mG<>JM&-h8IaWGDN~@15OucJ^(fi?%4nXZpJkDK*=6 znvBVS6falS1%_cP@Em1lm1QE{t?ZmKE8=`*-O6?#-mC1qvYm(vwpqmVD9i#V(!^dc zc|kUOpRx;@XG7ertWVi4#6dVh)2~blZk5Vm7nS8g2bB$^S;Uw;K(E3255m^Bn(ZvqvEof=mRZT2}{t7KN zs!Tf7p==DZY*P$B11+7ru6feEOK`Dq%`1T(P7`Ay6M%A1`3}NxY55Ih`=JNn((+qi z7{&?j(9TRMD+BX`NhfbBlW)kUc~i8BY+6|j*c)i++KjSV=w8D8i#=3$5IPT9+V)77ygiP0&&^W0qP&@Sw7JWzAqxLmf{nVs|O?EkX>_ z0(U|*B<2^Xh@4Wolqw&CT)vp($<}3@TuPV!bBWq`bD4_S6DNB{wc%rNYPLz$a&In^ zn2J*eQA6VQG94$Y1*=HU79t?Y01x%TK$)@Sjzx zvkpRs*tJGs8sgbC>Pc*^QD+|c*psAU3h;>}`N_lgW%BW-Bz0zn+nI?=C)a73 zy!>sQib{P=Zm(&>yYOETtvThi3{1AwW>J5W+cWoz>|B_lkj3ZraaRhP1^7vd8ZAM$ Yhf^tvlAl{Ps029w3)vH+8|3)$ADq&TtpET3 diff --git a/src/components/header.tsx b/src/components/header.tsx index b1db00e..20167a6 100644 --- a/src/components/header.tsx +++ b/src/components/header.tsx @@ -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; @@ -24,17 +26,19 @@ export const Header = ({ History -
  • - - Logout - -
  • + {!allowUnauthenticated ? ( +
  • + + Logout + +
  • + ) : null} ); } else { diff --git a/src/index.tsx b/src/index.tsx index c609af2..859ffb4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -215,6 +215,7 @@ const app = new Elysia({
    @@ -340,6 +341,7 @@ const app = new Elysia({
    @@ -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, @@ -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) { @@ -547,7 +551,11 @@ const app = new Elysia({ return ( <> -
    +

    Convert

    @@ -951,7 +959,11 @@ const app = new Elysia({ return ( <> -
    +

    Results

    @@ -1038,7 +1050,11 @@ const app = new Elysia({ return ( <> -
    +
    @@ -1284,7 +1300,11 @@ const app = new Elysia({ return ( <> -
    +

    Converters