Skip to content

Commit

Permalink
feat: config in handle auth fn
Browse files Browse the repository at this point in the history
  • Loading branch information
peterphanouvong committed Jun 24, 2024
1 parent a9ddacb commit c2f9d63
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 26 deletions.
26 changes: 17 additions & 9 deletions src/handlers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,29 @@ const getRoute = (endpoint) => {
/**
* @param {object} [request]
* @param {string} [endpoint]
* @param {{onError: (error: Error) => void}} [options]
* @param {{onError?: () => void; config: {audience?: string | string[], clientId?: string, clientSecret?: string, issuerURL?: string, siteUrl?: string, postLoginRedirectUrl?: string, postLogoutRedirectUrl?: string}}} options
* @returns {(req, res) => any}
*/
export default (request, endpoint, options) => {
if (!config.clientOptions.authDomain)
throw new Error("env variable 'KINDE_ISSUER_URL' is not set");
if (!config.clientOptions.authDomain && !options?.config?.issuerURL)
throw new Error(
"env variable 'KINDE_ISSUER_URL' is not set and not passed in options"
);

if (!config.clientOptions.clientId)
throw new Error("env variable 'KINDE_CLIENT_ID' is not set");
if (!config.clientOptions.clientId && !options?.config?.clientId)
throw new Error(
"env variable 'KINDE_CLIENT_ID' is not set and not passed in options"
);

if (!config.clientOptions.clientSecret)
throw new Error("env variable 'KINDE_CLIENT_SECRET' is not set");
if (!config.clientOptions.clientSecret && !options?.config?.clientSecret)
throw new Error(
"env variable 'KINDE_CLIENT_SECRET' is not set and not passed in options"
);

if (!config.clientOptions.redirectURL)
throw new Error("env variable 'KINDE_SITE_URL' is not set");
if (!config.clientOptions.redirectURL && !options?.config?.siteUrl)
throw new Error(
"env variable 'KINDE_SITE_URL' is not set and not passed in options"
);

// For backwards compatibility in app router
if (typeof request == 'object' && typeof endpoint == 'string') {
Expand Down
5 changes: 2 additions & 3 deletions src/handlers/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export const callback = async (routerClient) => {
return routerClient.json({error: error.message}, {status: 500});
}

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

return routerClient.redirect(config.redirectURL);
return routerClient.redirect(routerClient.clientConfig.siteUrl);
};
19 changes: 17 additions & 2 deletions src/routerClients/AppRouterClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@ export default class AppRouterClient extends RouterClient {
*
* @param {NextRequest} req
* @param {*} res
* @param {{onError?: () => void}} options
* @param {{onError?: () => void; config: {audience?: string | string[], clientId?: string, clientSecret?: string, issuerURL?: string, siteUrl?: string, postLoginRedirectUrl?: string, postLogoutRedirectUrl?: string}}} options
*/
constructor(req, res, options) {
super();
this.clientConfig = {
...config.clientOptions,
audience: options?.config?.audience || config.clientOptions.audience,
authDomain: options?.config?.issuerURL || config.clientOptions.authDomain,
clientId: options?.config?.clientId || config.clientOptions.clientId,
clientSecret:
options?.config?.clientSecret || config.clientOptions.clientSecret,
logoutRedirectURL:
options?.config?.postLogoutRedirectUrl ||
config.clientOptions.logoutRedirectURL,
redirectURL:
`${options?.config?.siteUrl}/api/auth/kinde_callback` ||
config.clientOptions.redirectURL,
siteUrl: config.redirectURL || options.config.siteUrl
};
this.kindeClient = createKindeServerClient(
config.grantType,
config.clientOptions
this.clientConfig
);
this.url = new URL(req.url);
this.sessionManager = appRouterSessionManager(cookies());
Expand Down
31 changes: 19 additions & 12 deletions src/routerClients/PagesRouterClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,33 @@ export default class PagesRouterClient extends RouterClient {
*
* @param {import('next').NextApiRequest} req
* @param {import('next').NextApiResponse} res
* @param {{onError?: () => void; config: {audience?: string | string[], clientId?: string, clientSecret?: string, issuerURL?: string, siteUrl?: string, postLoginRedirectUrl?: string, postLogoutRedirectUrl?: string}}} options
*/
constructor(req, res, clientOptions) {
constructor(req, res, options) {
super();
const url = req.url.split('/');
url.pop();

this.clientConfig = {
...config.clientOptions,
audience: options?.config?.audience || config.clientOptions.audience,
authDomain: options?.config?.issuerURL || config.clientOptions.authDomain,
clientId: options?.config?.clientId || config.clientOptions.clientId,
clientSecret:
options?.config?.clientSecret || config.clientOptions.clientSecret,
logoutRedirectURL:
options?.config?.postLogoutRedirectUrl ||
config.clientOptions.logoutRedirectURL,
redirectURL:
`${options?.config?.siteUrl}/api/auth/kinde_callback` ||
config.clientOptions.redirectURL,
siteUrl: config.redirectURL || options.config.siteUrl
};
this.kindeClient = createKindeServerClient(
config.grantType,
clientOptions
? {
...config.clientOptions,
authDomain: clientOptions.domain || '',
clientId: clientOptions.clientId || '',
clientSecret: clientOptions.clientSecret || '',
redirectURL: `${config.redirectURL}${url.join('/')}/kinde_callback`
}
: config.clientOptions
this.clientConfig
);
// @ts-ignore
this.url = new URL(config.redirectURL + req.url);
this.url = new URL(this.clientConfig.siteUrl + req.url);
this.res = res;
this.req = req;
this.searchParams = this.url.searchParams;
Expand Down
2 changes: 2 additions & 0 deletions src/routerClients/RouterClient.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {NextResponse} from 'next/server';

export default class RouterClient {
/** @type {import('../../types').KindeClientConfig} */
clientConfig = {};
/** @type {import('../../types').KindeClient} */
kindeClient = null;
/** @type {URL} */
Expand Down
15 changes: 15 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,18 @@ export type KindeRoles = {
key: string;
name: string;
}[];

export type KindeClientConfig = {
redirectURL: string;
audience: string | string[];
clientId: string;
clientSecret: string;
issuerURL?: string;
siteUrl?: string;
postLoginRedirectUrl?: string;
postLogoutRedirectUrl?: string;
authDomain: any;
logoutRedirectURL: any;
frameworkVersion: string;
framework: string;
};

0 comments on commit c2f9d63

Please sign in to comment.