Skip to content

Add multi-instance support and session management in nextjs#394

Draft
kavindadimuthu wants to merge 7 commits intoasgardeo:mainfrom
kavindadimuthu:feature/nextjs-multiorg
Draft

Add multi-instance support and session management in nextjs#394
kavindadimuthu wants to merge 7 commits intoasgardeo:mainfrom
kavindadimuthu:feature/nextjs-multiorg

Conversation

@kavindadimuthu
Copy link
Contributor

Purpose

This pull request introduces support for multiple independent authentication client instances ("multiton" pattern) in the Asgardeo Next.js integration, allowing different parts of an application to use separate authentication contexts. The changes refactor the client and server provider logic, update server actions and tests to handle instance IDs, and ensure all relevant functions and actions are instance-aware.

Client instance management:

  • Refactored AsgardeoNextClient from singleton to multiton, enabling multiple client instances identified by an instanceId. Added methods to create, destroy, and check existence of instances, and store the instanceId in each instance. (packages/nextjs/src/AsgardeoNextClient.ts)
  • Updated the client provider (AsgardeoClientProvider) and server provider (AsgardeoServerProvider) components to accept and propagate instanceId, ensuring client and server logic operate on the correct instance. (packages/nextjs/src/client/contexts/Asgardeo/AsgardeoProvider.tsx, packages/nextjs/src/server/AsgardeoProvider.tsx) [1] [2]

Server actions and session management:

  • Modified all server actions (createOrganization, getAllOrganizations, getCurrentOrganizationAction, etc.) to accept and use instanceId, ensuring actions interact with the correct client instance and session. (packages/nextjs/src/server/actions/createOrganization.ts, packages/nextjs/src/server/actions/getAllOrganizations.ts, packages/nextjs/src/server/actions/getCurrentOrganizationAction.ts) [1] [2] [3]
  • Updated session and token retrieval utilities to be instance-aware, passing instanceId where needed. (packages/nextjs/src/server/AsgardeoProvider.tsx, packages/nextjs/src/server/actions/getAccessToken.ts) [1] [2]

OAuth callback and action binding:

  • Enhanced OAuth callback handling in the client provider to check the instanceId embedded in the callback state, ensuring only the correct instance processes the callback. (packages/nextjs/src/client/contexts/Asgardeo/AsgardeoProvider.tsx)
  • Bound server actions to their respective instance IDs before passing to client components, enabling React serialization and correct invocation. (packages/nextjs/src/server/AsgardeoProvider.tsx)

Testing:

  • Updated all relevant tests for server actions to include and verify instanceId handling, ensuring correctness of the new multiton logic. (packages/nextjs/src/server/actions/__tests__/createOrganization.test.ts, packages/nextjs/src/server/actions/__tests__/getAllOrganizations.test.ts) [1] [2]

These changes collectively enable robust support for multiple authentication contexts within a Next.js application, improving flexibility for multi-tenant or complex authentication scenarios.

Related Issues

  • N/A

Related PRs

  • N/A

Checklist

  • Followed the CONTRIBUTING guidelines.
  • Manual test round performed and verified.
  • Documentation provided. (Add links if there are any)
  • Unit tests provided. (Add links if there are any)

Security checks

…session management utilities for instance handling

- Step 1, 2, 3
- Step 4
- Updated `isSignedIn` to accept an optional instance ID for better session management.
- Modified `signInAction`, `signOutAction`, `signUpAction`, `switchOrganization`, and `updateUserProfileAction` to include instance ID handling.
- Ensured that session cookies are managed per instance to support multi-instance scenarios.
- Improved error handling and logging across actions for better debugging.
- Refactor createOrganization function to improve parameter order and maintain code consistency
…ession handling

- Updated getAllOrganizations, handleOAuthCallbackAction, signInAction, signUpAction, switchOrganization, and updateUserProfileAction to accept instanceId as a parameter for better session management.
- Enhanced asgardeo middleware to support instanceId, ensuring consistent session handling across requests.
- Improved error handling and logging in various actions to provide clearer feedback on failures.
@asgardeo-github-bot
Copy link

⚠️ No Changeset found

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go.

If these changes should result in a version bump, you need to add a changeset.

Refer Release Documentation to learn how to add a changeset.

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.

2 participants