From 722af2d16b992d7141b8e8357f878fb329a7fbf8 Mon Sep 17 00:00:00 2001 From: krutoo Date: Sun, 8 Dec 2024 20:43:54 +0500 Subject: [PATCH] jwt format - `middleware/jwt`: option `format` added, now middleware can be used with different formats of tokens, for example Telegram Mini App format `tma ` --- src/middleware/jwt.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/middleware/jwt.ts b/src/middleware/jwt.ts index b4893a0..23291e7 100644 --- a/src/middleware/jwt.ts +++ b/src/middleware/jwt.ts @@ -7,6 +7,9 @@ export interface JwtMiddlewareOptions { /** Filter. Takes request, should return boolean. When returns false, JWT payload will not be added to request. */ filter?: (request: Request) => boolean; + + /** Allows to change default format "Bearer {accessToken}" of "Authorization" header value. */ + format?: (token: string) => string; } /** @@ -17,6 +20,7 @@ export interface JwtMiddlewareOptions { export function jwt({ token, filter = () => true, + format = (tokenValue) => `Bearer ${tokenValue}`, }: JwtMiddlewareOptions): Middleware { const getToken = typeof token === 'function' ? token : () => token; @@ -31,7 +35,7 @@ export function jwt({ const token = await getToken(); if (typeof token === 'string') { - headers.set('Authorization', `Bearer ${token}`); + headers.set('Authorization', format(token)); } return next(new Request(request, { headers }));