From 580c66e96e69ba8a56454b45976688c4b6b56f22 Mon Sep 17 00:00:00 2001 From: Peter Phanouvong Date: Wed, 12 Jun 2024 14:29:39 +1000 Subject: [PATCH 1/2] feat: on error callback fn --- src/handlers/auth.js | 25 ++++++++++++++++++++----- src/handlers/callback.js | 16 +++++++++++----- src/routerClients/AppRouterClient.js | 10 +++++++++- src/routerClients/RouterClient.js | 8 ++++++-- 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/handlers/auth.js b/src/handlers/auth.js index 34777ac..01fbee6 100644 --- a/src/handlers/auth.js +++ b/src/handlers/auth.js @@ -10,6 +10,7 @@ import AppRouterClient from '../routerClients/AppRouterClient'; import PagesRouterClient from '../routerClients/PagesRouterClient'; import {NextRequest} from 'next/server'; import RouterClient from '../routerClients/RouterClient'; +import {config} from '../config/index'; /** * @type {Record Promise>} @@ -35,13 +36,26 @@ const getRoute = (endpoint) => { /** * @param {object} [request] * @param {string} [endpoint] + * @param {{onError: (error: Error) => void}} [options] * @returns {(req, res) => any} */ -export default (request, endpoint) => { +export default (request, endpoint, options) => { + if (!config.clientOptions.authDomain) + throw new Error("env variable 'KINDE_ISSUER_URL' is not set"); + + if (!config.clientOptions.clientId) + throw new Error("env variable 'KINDE_CLIENT_ID' is not set"); + + if (!config.clientOptions.clientSecret) + throw new Error("env variable 'KINDE_CLIENT_SECRET' is not set"); + + if (!config.clientOptions.redirectURL) + throw new Error("env variable 'KINDE_SITE_URL' is not set"); + // For backwards compatibility in app router if (typeof request == 'object' && typeof endpoint == 'string') { // @ts-ignore - return appRouterHandler(request, {params: {kindeAuth: endpoint}}); + return appRouterHandler(request, {params: {kindeAuth: endpoint}}, options); } /** * @@ -51,7 +65,7 @@ export default (request, endpoint) => { return async function handler(req, res) { return isAppRouter(req) ? // @ts-ignore - appRouterHandler(req, res) + appRouterHandler(req, res, options) : // @ts-ignore pagesRouterHandler(req, res, request); }; @@ -61,9 +75,10 @@ export default (request, endpoint) => { * * @param {NextRequest} req * @param {{params: {kindeAuth: string}}} res + * @param {{onError?: () => void}} options * @returns */ -const appRouterHandler = async (req, res) => { +const appRouterHandler = async (req, res, options) => { const {params} = res; let endpoint = params.kindeAuth; endpoint = Array.isArray(endpoint) ? endpoint[0] : endpoint; @@ -71,7 +86,7 @@ const appRouterHandler = async (req, res) => { return route ? // @ts-ignore - await route(new AppRouterClient(req, res)) + await route(new AppRouterClient(req, res, options)) : new Response('This page could not be found.', {status: 404}); }; diff --git a/src/handlers/callback.js b/src/handlers/callback.js index 19f7803..ef04d5c 100644 --- a/src/handlers/callback.js +++ b/src/handlers/callback.js @@ -16,12 +16,18 @@ export const callback = async (routerClient) => { const postLoginRedirectURL = postLoginRedirectURLFromMemory ? postLoginRedirectURLFromMemory : config.postLoginRedirectURL; - - await routerClient.kindeClient.handleRedirectToApp( - routerClient.sessionManager, - routerClient.getUrl() - ); + try { + await routerClient.kindeClient.handleRedirectToApp( + routerClient.sessionManager, + routerClient.getUrl() + ); + } catch (error) { + routerClient.onError(error); + return routerClient.json({error: error.message}, {status: 500}); + } if (typeof postLoginRedirectURL === 'string') return routerClient.redirect(postLoginRedirectURL); + + return routerClient.redirect(config.redirectURL); }; diff --git a/src/routerClients/AppRouterClient.js b/src/routerClients/AppRouterClient.js index 26ca105..4ad3c35 100644 --- a/src/routerClients/AppRouterClient.js +++ b/src/routerClients/AppRouterClient.js @@ -11,8 +11,9 @@ export default class AppRouterClient extends RouterClient { * * @param {NextRequest} req * @param {*} res + * @param {{onError?: () => void}} options */ - constructor(req, res) { + constructor(req, res, options) { super(); this.kindeClient = createKindeServerClient( config.grantType, @@ -22,6 +23,7 @@ export default class AppRouterClient extends RouterClient { this.sessionManager = appRouterSessionManager(cookies()); this.req = req; this.searchParams = req.nextUrl.searchParams; + this.onErrorCallback = options?.onError; } /** @@ -63,4 +65,10 @@ export default class AppRouterClient extends RouterClient { getSearchParam(key) { return this.req.nextUrl.searchParams.get(key); } + + onError(error) { + if (this.onErrorCallback) { + this.onErrorCallback(error); + } + } } diff --git a/src/routerClients/RouterClient.js b/src/routerClients/RouterClient.js index 513d70a..2b18943 100644 --- a/src/routerClients/RouterClient.js +++ b/src/routerClients/RouterClient.js @@ -13,12 +13,12 @@ export default class RouterClient { req; /** @type {URLSearchParams} */ searchParams; - + constructor() { if (this.constructor == RouterClient) { throw new Error("Abstract classes can't be instantiated."); } - } + } /** * @@ -59,4 +59,8 @@ export default class RouterClient { getSearchParam(key) { throw new Error("Method 'getSearchParam()' must be implemented."); } + + onError() { + throw new Error("Method 'onError()' must be implemented."); + } } From ac2a2cfa1cd28252d64b56e4266f7fb44a2048bc Mon Sep 17 00:00:00 2001 From: Daniel Rivers Date: Mon, 17 Jun 2024 15:50:20 +0100 Subject: [PATCH 2/2] chore: remove invalid line --- src/handlers/callback.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/handlers/callback.js b/src/handlers/callback.js index ef04d5c..9838a89 100644 --- a/src/handlers/callback.js +++ b/src/handlers/callback.js @@ -22,7 +22,6 @@ export const callback = async (routerClient) => { routerClient.getUrl() ); } catch (error) { - routerClient.onError(error); return routerClient.json({error: error.message}, {status: 500}); }