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

types: fix permission types and userOrg default value #228

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

peterphanouvong
Copy link
Collaborator

@peterphanouvong peterphanouvong commented Oct 28, 2024

Explain your changes

Checklist

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

Copy link
Contributor

coderabbitai bot commented Oct 28, 2024

Walkthrough

The changes in this pull request involve modifications to the initialState object in src/config/index.ts and updates to the KindeState type in types.d.ts. The initialState now has an explicit type definition and includes new properties for token management, while the userOrganizations property has been altered to null. Additionally, the KindeState type's permissions property and the getPermissions method's return type have been updated to support more flexible permission handling.

Changes

File Change Summary
src/config/index.ts - Updated initialState type to KindeState.
- Changed userOrganizations from [] to null.
- Added properties: accessTokenEncoded, accessTokenRaw, idTokenRaw, idTokenEncoded, all initialized to null.
- Introduced methods: getAccessTokenRaw and getIdTokenRaw, both returning null.
types.d.ts - Updated permissions type from KindePermissions to `KindePermissions

Possibly related PRs

  • fix: setup get user object + to ts #205: This PR modifies the setup function to change how user properties are handled, which may relate to the overall state management and token handling introduced in the main PR.
  • types: allow void for generic type #227: This PR updates the KindeUser interface in types.d.ts, which is relevant as it pertains to type definitions that may interact with the KindeState type modified in the main PR.

Suggested reviewers

  • DaveOrDead

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 eslint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

types.d.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-jest".

(The package "eslint-plugin-jest" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-jest@latest --save-dev

The plugin "eslint-plugin-jest" was referenced from the config file in ".eslintrc.json".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between caaea25 and a29e63c.

📒 Files selected for processing (1)
  • types.d.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • types.d.ts

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between cbdfe85 and caaea25.

📒 Files selected for processing (2)
  • src/config/index.ts (2 hunks)
  • types.d.ts (2 hunks)
🔇 Additional comments (6)
src/config/index.ts (5)

3-3: LGTM: Clean type import addition

The KindeState type import is properly added, improving type safety.


5-5: LGTM: Proper type annotation added

The explicit KindeState type annotation improves type safety and code maintainability.


28-33: LGTM: Consistent token property additions

The new token-related properties and methods follow a consistent pattern:

  • All properties initialized as null
  • Corresponding getter methods returning null
  • Clear naming convention

13-13: ⚠️ Potential issue

Breaking Change: userOrganizations default value

Changing userOrganizations from [] to null is a breaking change that could affect null checks in consuming code. Consider:

  1. Adding a migration guide
  2. Documenting this change in the changelog
#!/bin/bash
# Search for userOrganizations usage patterns
echo "Checking userOrganizations access patterns:"
rg -A 2 'userOrganizations\.' --type typescript

echo -e "\nChecking null checks on userOrganizations:"
rg -A 2 'userOrganizations(\s+)?(!=|==|===|!==)(\s+)?(null|\[\])' --type typescript

11-11: Verify permission type consistency

The permission initialization and getter return type align with the updated KindeState type. Let's verify this change is consistent across the codebase.

Also applies to: 20-20

✅ Verification successful

Let me try a different approach to search for permission-related patterns in the codebase. The previous command had issues with the TypeScript file type recognition, so I'm using more generic ripgrep patterns without file type filters.


Permission type initialization is consistent across the codebase

The empty array initialization for permissions is consistent with the codebase's type definitions and usage patterns:

  • Type definitions in types.d.ts show permissions: KindePermissions | [] in the state type
  • Initial states in both src/config/index.ts and src/frontend/KindeBrowserClient.js use permissions: []
  • Return types in getPermissions methods correctly handle the KindePermissions | [] type
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for permission type definitions and usages
echo "Checking permission type definitions:"
ast-grep --pattern 'type $_ = { $$$ permissions: $_ $$$ }'

echo -e "\nChecking permission property access patterns:"
rg -A 2 'permissions:' --type typescript

Length of output: 403


Script:

#!/bin/bash
# Search for permission type definitions and usages with ripgrep
echo "Checking permission type definitions:"
rg "type.*\{.*permissions.*\}" -A 3

echo -e "\nChecking permission property access and usage:"
rg "permissions(\s*):(\s*)" -A 2

echo -e "\nChecking permission type imports:"
rg "import.*Permission" -A 1

Length of output: 2560

types.d.ts (1)

306-306: Verify the impact of permission type changes across the codebase.

Let's ensure these type changes don't break existing permission checks in the codebase.

Also applies to: 340-340

✅ Verification successful

Type change appears safe and properly handled across the codebase

The verification shows that the KindePermissions type is used consistently and safely:

  • Type is only defined in types.d.ts and referenced in key authentication files
  • Permission checks in protect.js properly handle both array cases
  • Frontend components in KindeBrowserClient.js and AuthProvider.jsx safely access permissions through optional chaining
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for permission-related type usage patterns
echo "Checking permission type usage patterns..."
rg -l "KindePermissions" --type typescript

echo "\nChecking permission access patterns..."
rg "getPermissions\(\)" --type typescript -B 2 -A 2

echo "\nChecking direct permission property access..."
rg "\.permissions\." --type typescript -B 2 -A 2

Length of output: 543


Script:

#!/bin/bash
# Search for permission-related type usage patterns without file type filter
echo "Checking permission type usage patterns..."
rg -l "KindePermissions"

echo "\nChecking permission access patterns..."
rg "getPermissions\(\)" -B 2 -A 2

echo "\nChecking direct permission property access..."
rg "\.permissions\." -B 2 -A 2

# Also check for any TypeScript/JavaScript files that might use these patterns
echo "\nListing relevant files..."
fd "\.(ts|tsx|js|jsx|d\.ts)$"

Length of output: 4699

types.d.ts Outdated
@@ -303,7 +303,7 @@ export type KindeState = {
isAuthenticated: boolean | null;
isLoading: boolean | null;
organization: KindeOrganization;
permissions: KindePermissions;
permissions: KindePermissions | [];
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Consider using a more specific union type for permissions.

The current type KindePermissions | [] could lead to runtime errors when code expects the permissions and orgCode properties. Consider using a more type-safe alternative:

-  permissions: KindePermissions | [];
+  permissions: KindePermissions | { permissions: []; orgCode: null };

This ensures that even when empty, the permissions object maintains a consistent structure, preventing potential runtime errors.

📝 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
permissions: KindePermissions | [];
permissions: KindePermissions | { permissions: []; orgCode: null };

types.d.ts Outdated
@@ -337,7 +337,7 @@ export type KindeState = {
getPermission: (
key: string
) => {isGranted: boolean; orgCode: string | null} | null;
getPermissions: () => KindePermissions;
getPermissions: () => KindePermissions | [];
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure consistent typing between permissions property and getPermissions method.

The return type of getPermissions should match the type of the permissions property. If we update the property type as suggested above, we should also update this method:

-  getPermissions: () => KindePermissions | [];
+  getPermissions: () => KindePermissions | { permissions: []; orgCode: null };

This maintains type consistency and prevents potential type mismatches between the property and its getter method.

Committable suggestion was skipped due to low confidence.

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