From 9cb0f01060769668bd95ea9139ddc0f03ee094fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=C2=A0F=2E=20Romaniello?= Date: Fri, 1 Oct 2021 17:00:38 -0300 Subject: [PATCH] fix: next.js types --- packages/next/src/index.ts | 77 ++++++++++++++++++++------------------ packages/next/src/types.ts | 4 -- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/packages/next/src/index.ts b/packages/next/src/index.ts index 7f64bf0..b21c34e 100644 --- a/packages/next/src/index.ts +++ b/packages/next/src/index.ts @@ -1,51 +1,54 @@ -import { NextApiResponse, NextApiRequest } from 'next'; -import { JwtVerifier, getTokenFromHeader } from '@serverless-jwt/jwt-verifier'; -import { NextJwtVerifierOptions, IApiRoute, NextAuthenticatedApiRequest } from './types'; +import { NextApiResponse, NextApiRequest, NextApiHandler } from 'next'; +import { JwtVerifier, getTokenFromHeader, JwtVerifierError } from '@serverless-jwt/jwt-verifier'; +import { NextJwtVerifierOptions, NextAuthenticatedApiRequest } from './types'; /** * Middleware to validate a token and set the user context. */ const validateJWT = (verifier: JwtVerifier, options: NextJwtVerifierOptions) => { - return (apiRoute: IApiRoute): IApiRoute => async (req: NextApiRequest, res: NextApiResponse): Promise => { - if (!req) { - throw new Error('Request is not available'); - } - - if (!res) { - throw new Error('Response is not available'); - } - - let claims; - let accessToken; - - try { - accessToken = getTokenFromHeader(req.headers.authorization as string); - claims = await verifier.verifyAccessToken(accessToken); - } catch (err) { - if (typeof options.handleError !== 'undefined' && options.handleError !== null) { - return options.handleError(res, err); + return (handler: NextApiHandler): NextApiHandler => { + return async (req: NextApiRequest, res: NextApiResponse) => { + if (!req) { + throw new Error('Request is not available'); } - return res.status(401).json({ - error: err.code, - error_description: err.message - }); - } - - // Expose the identity in the client context. - const authenticatedRequest = req as NextAuthenticatedApiRequest; - authenticatedRequest.identityContext = { - token: accessToken as string, - claims: claims as Record - }; + if (!res) { + throw new Error('Response is not available'); + } + + let claims; + let accessToken; + + try { + accessToken = getTokenFromHeader(req.headers.authorization as string); + claims = await verifier.verifyAccessToken(accessToken); + } catch (err) { + if (typeof options.handleError !== 'undefined' && options.handleError !== null) { + return options.handleError(res, err as JwtVerifierError); + } + + const verifyError = err as JwtVerifierError; + return res.status(401).json({ + error: verifyError.code, + error_description: verifyError.message + }); + } + + // Expose the identity in the client context. + const authenticatedRequest = req as NextAuthenticatedApiRequest; + authenticatedRequest.identityContext = { + token: accessToken as string, + claims: claims as Record + }; - // Continue. - return apiRoute(req, res); + // Continue. + return handler(req, res); + }; }; }; -export interface NextJwtVerifier { - (apiRoute: IApiRoute): (req: NextApiRequest, res: NextApiResponse) => Promise; +interface NextJwtVerifier { + (handler: NextApiHandler): NextApiHandler; } /** diff --git a/packages/next/src/types.ts b/packages/next/src/types.ts index a3e5d2f..f58c791 100644 --- a/packages/next/src/types.ts +++ b/packages/next/src/types.ts @@ -8,10 +8,6 @@ export interface NextJwtVerifierOptions extends JwtVerifierOptions { handleError?(res: NextApiResponse, err: Error | JwtVerifierError): Promise; } -export interface IApiRoute { - (req: NextApiRequest, res: NextApiResponse): Promise; -} - export interface IdentityContext { /** * The token that was provided.