-
Notifications
You must be signed in to change notification settings - Fork 610
fix: improve session handling to avoid calling session refresh logic consecutively #3083
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0e014d8
leverage RSC for auth util
mcstepp 6d4db78
fmt
mcstepp b3cad47
don't use RSC in routes, dummy
mcstepp 5e05b66
protect the new page better
mcstepp c607f89
[autofix.ci] apply automated fixes
autofix-ci[bot] 1d37618
remove variable assignment, add comments explaining why we need an au…
mcstepp bfd5984
remove unused variable
mcstepp d3435e3
[autofix.ci] apply automated fixes
autofix-ci[bot] 0e127fd
Merge branch 'main' into fix-middleware
mcstepp c71ab45
[autofix.ci] apply automated fixes
autofix-ci[bot] 8d7f502
update impersonator to use cached getCurrentUser
mcstepp 1235ac1
use an non-redirecting auth check on public pages
mcstepp f37eb31
[autofix.ci] apply automated fixes
autofix-ci[bot] b1463f4
Merge branch 'main' into fix-middleware
mcstepp 789e641
Merge branch 'main' into fix-middleware
mcstepp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,77 @@ | ||
| import { getAuth as noCacheGetAuth } from "@/lib/auth/get-auth"; | ||
| import { auth } from "@/lib/auth/server"; | ||
| import type { User } from "@/lib/auth/types"; | ||
| import { redirect } from "next/navigation"; | ||
| import { cache } from "react"; | ||
|
|
||
| type GetAuthResult = { | ||
| userId: string | null; | ||
| orgId: string | null; | ||
| }; | ||
|
|
||
| export async function getIsImpersonator(): Promise<boolean> { | ||
| const user = await getCurrentUser(); | ||
| return user.impersonator !== undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Return the org id or a 404 not found page. | ||
| * Validates the current user session and performs token refresh if needed. | ||
| * | ||
| * This function checks for a valid authentication cookie, validates the session, | ||
| * and handles token refreshing if the current token is expired but refreshable. | ||
| * Results are cached for the duration of the server request to prevent | ||
| * multiple validation calls. | ||
| * | ||
| * The auth check should already be done at a higher level, and we're just returning 404 to make typescript happy. | ||
| * @param _req - Optional request object (not used but maintained for compatibility) | ||
| * @returns Authentication result containing userId and orgId if authenticated, null values otherwise | ||
| * @throws Redirects to sign-in or organization/workspace creation pages if requirements aren't met | ||
| */ | ||
| export async function getOrgId(): Promise<string> { | ||
| const user = await auth.getCurrentUser(); | ||
| if (!user) { | ||
| export const getAuth = cache(async (_req?: Request): Promise<GetAuthResult> => { | ||
| const authResult = await noCacheGetAuth(); | ||
| if (!authResult.userId) { | ||
| redirect("/auth/sign-in"); | ||
| } | ||
|
|
||
| const { orgId } = user; | ||
| return authResult; | ||
| }); | ||
|
|
||
| /** | ||
| * Retrieves the current organization ID or redirects if unavailable. | ||
| * | ||
| * This function checks authentication status and organization membership. | ||
| * It will redirect to the sign-in page if the user is not authenticated, | ||
| * or to the workspace creation page if the user has no organization. | ||
| * Results are cached for the duration of the server request. | ||
| * | ||
| * @returns The current user's organization ID | ||
| */ | ||
| export const getOrgId = cache(async (): Promise<string> => { | ||
| const { orgId } = await getAuth(); | ||
|
|
||
| if (!orgId) { | ||
| redirect("/new"); | ||
| } | ||
|
|
||
| return orgId; | ||
| } | ||
| }); | ||
|
|
||
| export async function getIsImpersonator(): Promise<boolean> { | ||
| const user = await auth.getCurrentUser(); | ||
| /** | ||
| * Retrieves the complete current user object with organization information. | ||
| * | ||
| * This function fetches the authenticated user from the database along with | ||
| * their organization ID. It will redirect to the sign-in page if the user | ||
| * is not authenticated or cannot be found in the database. | ||
| * Results are cached for the duration of the server request. | ||
| * | ||
| * @returns Full user object with organization ID | ||
| * @throws Redirects to sign-in page if user is not authenticated or not found | ||
| */ | ||
| export const getCurrentUser = cache(async (): Promise<User> => { | ||
| const { userId, orgId } = await getAuth(); | ||
|
|
||
| const user = await auth.getUser(userId!); // getAuth will redirect if there's no userId | ||
| if (!user) { | ||
| return false; | ||
| redirect("/auth/sign-in"); | ||
| } | ||
| return user.impersonator !== undefined; | ||
| } | ||
| return { ...user, orgId }; | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.