From d6978f9873dd423d2e0f91096f07e055204d44ce Mon Sep 17 00:00:00 2001 From: dswbx Date: Tue, 3 Dec 2024 09:16:00 +0100 Subject: [PATCH] fix auth tests --- app/__test__/modules/AppAuth.spec.ts | 8 +++++++- app/src/auth/authenticate/Authenticator.ts | 9 +++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/__test__/modules/AppAuth.spec.ts b/app/__test__/modules/AppAuth.spec.ts index be1c7e1..14640f0 100644 --- a/app/__test__/modules/AppAuth.spec.ts +++ b/app/__test__/modules/AppAuth.spec.ts @@ -39,7 +39,10 @@ describe("AppAuth", () => { test("creates user on register", async () => { const auth = new AppAuth( { - enabled: true + enabled: true, + jwt: { + secret: "123456" + } }, ctx ); @@ -57,6 +60,9 @@ describe("AppAuth", () => { disableConsoleLog(); const res = await app.request("/password/register", { method: "POST", + headers: { + "Content-Type": "application/json" + }, body: JSON.stringify({ email: "some@body.com", password: "123456" diff --git a/app/src/auth/authenticate/Authenticator.ts b/app/src/auth/authenticate/Authenticator.ts index 9040440..426023b 100644 --- a/app/src/auth/authenticate/Authenticator.ts +++ b/app/src/auth/authenticate/Authenticator.ts @@ -11,7 +11,7 @@ import { } from "core/utils"; import type { Context, Hono } from "hono"; import { deleteCookie, getSignedCookie, setSignedCookie } from "hono/cookie"; -import { decode, sign, verify } from "hono/jwt"; +import { sign, verify } from "hono/jwt"; import type { CookieOptions } from "hono/utils/cookie"; import { omit } from "lodash-es"; @@ -177,7 +177,12 @@ export class Authenticator = Record< payload.exp = Math.floor(Date.now() / 1000) + this.config.jwt.expires; } - return sign(payload, this.config.jwt?.secret ?? "", this.config.jwt?.alg ?? "HS256"); + const secret = this.config.jwt.secret; + if (!secret || secret.length === 0) { + throw new Error("Cannot sign JWT without a secret"); + } + + return sign(payload, secret, this.config.jwt?.alg ?? "HS256"); } async verify(jwt: string): Promise {