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: config in handle auth fn #183

Merged
merged 1 commit into from
Jun 26, 2024
Merged

Conversation

peterphanouvong
Copy link
Collaborator

Explain your changes

  • allow passing env vairables (config) through handle auth fn
    Suppose there is a related issue with enough detail for a reviewer to understand your changes fully. In that case, you can omit an explanation and instead include either “Fixes #XX” or “Updates #XX” where “XX” is the issue number.

Checklist

🛟 If you need help, consider asking for advice over in the Kinde community.

Copy link
Contributor

coderabbitai bot commented Jun 24, 2024

Walkthrough

The recent updates improve configuration management and error handling across the authentication and routing modules. Key enhancements include standardizing functions to use an options object for configuration details, updating redirection logic, and introducing new configuration types. These changes ensure more structured, reliable, and maintainable code by centralizing configuration details and improving error validation.

Changes

File Path Change Summary
src/handlers/auth.js Updated function parameters to expect an options object and enhanced error handling logic.
src/handlers/callback.js Modified redirection logic to use postLoginRedirectURL and routerClient.clientConfig.siteUrl.
src/routerClients/AppRouterClient.js Introduced a more detailed options object and updated clientConfig initialization.
src/routerClients/PagesRouterClient.js Changed constructor parameter from clientOptions to options and updated clientConfig initialization.
src/routerClients/RouterClient.js Added a new clientConfig property of type KindeClientConfig to the RouterClient class.
types.d.ts Added a new KindeClientConfig type detailing client configuration properties.

Sequence Diagrams

Updated Callback Redirection Logic

Before

sequenceDiagram
    participant User
    participant AppRouterClient
    participant CallbackHandler

    User ->> AppRouterClient: Request login
    AppRouterClient ->> CallbackHandler: Redirect with postLoginRedirectURL
    CallbackHandler ->> User: Redirected based on config.redirectURL
Loading

After

sequenceDiagram
    participant User
    participant AppRouterClient
    participant CallbackHandler

    User ->> AppRouterClient: Request login
    AppRouterClient ->> CallbackHandler: Redirect with postLoginRedirectURL
    CallbackHandler ->> User: Redirected based on routerClient.clientConfig.siteUrl
Loading

Configuration Initialization in AppRouterClient

sequenceDiagram
    participant Request
    participant AppRouterClient
    participant KindeServerClient

    Request ->> AppRouterClient: Instantiate with options
    AppRouterClient ->> AppRouterClient: Initialize clientConfig
    AppRouterClient ->> KindeServerClient: create with clientConfig
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 8

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a9ddacb and c2f9d63.

Files selected for processing (6)
  • src/handlers/auth.js (1 hunks)
  • src/handlers/callback.js (1 hunks)
  • src/routerClients/AppRouterClient.js (1 hunks)
  • src/routerClients/PagesRouterClient.js (1 hunks)
  • src/routerClients/RouterClient.js (1 hunks)
  • types.d.ts (1 hunks)
Additional comments not posted (2)
src/handlers/callback.js (1)

28-30: Verify redirection logic consistency.

The changes to the redirection logic after login should be verified to ensure they align with the new configuration settings and intended user flow.

Verification successful

Verify redirection logic consistency.

The redirection URLs for postLoginRedirectURL and siteUrl have been correctly referenced and utilized across the codebase, aligning with the new configuration settings and intended user flow.

  • postLoginRedirectURL is defined and used appropriately in src/handlers/callback.js, src/handlers/login.js, src/handlers/health.js, src/components/RegisterLink.js, src/config/index.js, src/authMiddleware/authMiddleware.js, src/components/LoginLink.js, and src/handlers/register.js.
  • siteUrl is defined and used appropriately in src/utils/generateCallbackUrl.js, src/routerClients/PagesRouterClient.js, src/routerClients/AppRouterClient.js, src/handlers/callback.js, and src/handlers/auth.js.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the redirection URLs are being set correctly according to the new configuration.

# Test: Search for the usage of `postLoginRedirectURL` and `siteUrl`.
rg --type python 'postLoginRedirectURL' 'siteUrl'

Length of output: 78



Script:

#!/bin/bash
# Description: Verify that the redirection URLs are being set correctly according to the new configuration.

# Test: Search for the usage of `postLoginRedirectURL` and `siteUrl`.
rg --type js 'postLoginRedirectURL'
rg --type js 'siteUrl'

Length of output: 3506

src/routerClients/RouterClient.js (1)

4-5: Initialization of clientConfig.

Ensure that clientConfig is properly initialized and used throughout the class to maintain consistency and avoid potential null reference errors.

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update function documentation to reflect the new options parameter.

The function now accepts a more complex options parameter. Updating the documentation to explain the structure and usage of this parameter would enhance code maintainability and understandability.

Comment on lines +58 to +61
if (!config.clientOptions.redirectURL && !options?.config?.siteUrl)
throw new Error(
"env variable 'KINDE_SITE_URL' is not set and not passed in options"
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify the error message for missing site URL.

The current error message might confuse users as it mentions a different environment variable than what might be expected.

- "env variable 'KINDE_SITE_URL' is not set and not passed in options"
+ "The environment variable 'KINDE_SITE_URL' is required but not set. Please set it in your environment or pass it in the options."
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!config.clientOptions.redirectURL && !options?.config?.siteUrl)
throw new Error(
"env variable 'KINDE_SITE_URL' is not set and not passed in options"
);
if (!config.clientOptions.redirectURL && !options?.config?.siteUrl)
throw new Error(
"The environment variable 'KINDE_SITE_URL' is required but not set. Please set it in your environment or pass it in the options."
);

Comment on lines +43 to +46
if (!config.clientOptions.authDomain && !options?.config?.issuerURL)
throw new Error(
"env variable 'KINDE_ISSUER_URL' is not set and not passed in options"
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider refining the error message for clarity.

The error message could be more descriptive. Instead of just stating that the variable is not set, it could guide the user on how to set it or where to find more information.

- "env variable 'KINDE_ISSUER_URL' is not set and not passed in options"
+ "The environment variable 'KINDE_ISSUER_URL' is required but not set. Please set it in your environment or pass it in the options."
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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.authDomain && !options?.config?.issuerURL)
throw new Error(
"The environment variable 'KINDE_ISSUER_URL' is required but not set. Please set it in your environment or pass it in the options."
);

Comment on lines +48 to +51
if (!config.clientOptions.clientId && !options?.config?.clientId)
throw new Error(
"env variable 'KINDE_CLIENT_ID' is not set and not passed in options"
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistent error handling for missing configuration.

The error handling for missing configuration values is good, but ensure that the messages are consistent across different checks to help in troubleshooting.

- "env variable 'KINDE_CLIENT_ID' is not set and not passed in options"
+ "The environment variable 'KINDE_CLIENT_ID' is required but not set. Please set it in your environment or pass it in the options."
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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.clientId && !options?.config?.clientId)
throw new Error(
"The environment variable 'KINDE_CLIENT_ID' is required but not set. Please set it in your environment or pass it in the options."
);

Comment on lines +53 to +56
if (!config.clientOptions.clientSecret && !options?.config?.clientSecret)
throw new Error(
"env variable 'KINDE_CLIENT_SECRET' is not set and not passed in options"
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve error message for missing client secret.

Similar to previous comments, the error message can be made more informative.

- "env variable 'KINDE_CLIENT_SECRET' is not set and not passed in options"
+ "The environment variable 'KINDE_CLIENT_SECRET' is required but not set. Please set it in your environment or pass it in the options."
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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.clientSecret && !options?.config?.clientSecret)
throw new Error(
"The environment variable 'KINDE_CLIENT_SECRET' is required but not set. Please set it in your environment or pass it in the options."
);

Comment on lines +12 to +32
* @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
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure robust configuration handling.

The constructor has been modified significantly to handle a new configuration structure. Ensure that all configuration properties are correctly validated and have sensible defaults to prevent runtime issues.

- this.clientConfig = {
+ this.clientConfig = options?.config || config.clientOptions;

Committable suggestion was skipped due to low confidence.

Comment on lines +268 to +281
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;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review newly added KindeClientConfig type.

The new type KindeClientConfig is comprehensive and covers many necessary fields. However, ensure consistency in optional properties across the type definition to prevent potential runtime errors or misunderstandings.

-  issuerURL?: string;
+  issuerURL: string;
-  siteUrl?: string;
+  siteUrl: string;
-  postLoginRedirectUrl?: string;
+  postLoginRedirectUrl: string;
-  postLogoutRedirectUrl?: string;
+  postLogoutRedirectUrl: string;
-  authDomain: any;
+  authDomain: string;
-  logoutRedirectURL: any;
+  logoutRedirectURL: string;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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;
};
export type KindeClientConfig = {
redirectURL: string;
audience: string | string[];
clientId: string;
clientSecret: string;
issuerURL: string;
siteUrl: string;
postLoginRedirectUrl: string;
postLogoutRedirectUrl: string;
authDomain: string;
logoutRedirectURL: string;
frameworkVersion: string;
framework: string;
};

Comment on lines +14 to +32
* @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
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review configuration initialization in constructor.

The constructor now includes a more complex configuration object. Review and ensure that all properties are correctly initialized and that defaults are handled properly to avoid any configuration errors.

- this.clientConfig = {
+ this.clientConfig = options?.config || config.clientOptions;

Committable suggestion was skipped due to low confidence.

@peterphanouvong peterphanouvong merged commit af2e39a into main Jun 26, 2024
3 checks passed
peterphanouvong added a commit that referenced this pull request Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant