From 468e7d016d367eeffd904d4adf9c84e5642e2fd3 Mon Sep 17 00:00:00 2001 From: kimzuni Date: Fri, 27 Jun 2025 18:06:26 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=A7=20fix:=20Use=20default=20exp?= =?UTF-8?q?=20and=20nbf=20if=20missing=20in=20jwt.sign=20payload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes elysiajs#101 --- src/index.ts | 10 ++++++---- test/index.test.ts | 5 +---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index fc8614b..01433f0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -154,12 +154,14 @@ JWTOption) => { crit }) - if (morePayloadNbf !== undefined) { - jwt = jwt.setNotBefore(morePayloadNbf) + const setNbf = morePayloadNbf ?? nbf; + if (setNbf !== undefined) { + jwt = jwt.setNotBefore(setNbf) } - if (morePayloadExp !== undefined) { - jwt = jwt.setExpirationTime(morePayloadExp) + const setExp = morePayloadExp ?? exp; + if (setExp !== undefined) { + jwt = jwt.setExpirationTime(setExp) } return jwt.sign(key) diff --git a/test/index.test.ts b/test/index.test.ts index 664d8ac..c3a8391 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -27,10 +27,7 @@ describe('JWT Plugin', () => { .post( '/sign-token', ({ jwt, body }) => - jwt.sign({ - name: body.name, - exp: '30m' - }), + jwt.sign(body), { body: t.Object({ name: t.String() From 6b5b0081829ec1473a6374ae9b86c898af119978 Mon Sep 17 00:00:00 2001 From: kimzuni Date: Tue, 1 Jul 2025 00:19:11 +0900 Subject: [PATCH 2/2] fix: allow `exp: undefined` in `jwt.sign()` to disable default `exp` set in `jwt()` --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 01433f0..4da9f40 100644 --- a/src/index.ts +++ b/src/index.ts @@ -154,12 +154,12 @@ JWTOption) => { crit }) - const setNbf = morePayloadNbf ?? nbf; + const setNbf = "nbf" in morePayload ? morePayloadNbf : nbf; if (setNbf !== undefined) { jwt = jwt.setNotBefore(setNbf) } - const setExp = morePayloadExp ?? exp; + const setExp = "exp" in morePayload ? morePayloadExp : exp; if (setExp !== undefined) { jwt = jwt.setExpirationTime(setExp) }