Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: on error callback fn #179

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/handlers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,(routerClient: RouterClient) => Promise<void>>}
Expand All @@ -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);
}
/**
*
Expand All @@ -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);
};
Expand All @@ -61,17 +75,18 @@ 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;
const route = getRoute(endpoint);

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});
};

Expand Down
15 changes: 10 additions & 5 deletions src/handlers/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ 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) {
return routerClient.json({error: error.message}, {status: 500});
}

if (typeof postLoginRedirectURL === 'string')
return routerClient.redirect(postLoginRedirectURL);

return routerClient.redirect(config.redirectURL);
};
10 changes: 9 additions & 1 deletion src/routerClients/AppRouterClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}
}
8 changes: 6 additions & 2 deletions src/routerClients/RouterClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
}

/**
*
Expand Down Expand Up @@ -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.");
}
}
Loading