Skip to content

Commit 6fcb5c2

Browse files
feedbacl
1 parent 7b22750 commit 6fcb5c2

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

packages/web/src/actions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import JoinRequestApprovedEmail from "./emails/joinRequestApprovedEmail";
2626
import JoinRequestSubmittedEmail from "./emails/joinRequestSubmittedEmail";
2727
import { AGENTIC_SEARCH_TUTORIAL_DISMISSED_COOKIE_NAME, MOBILE_UNSUPPORTED_SPLASH_SCREEN_DISMISSED_COOKIE_NAME, SOURCEBOT_SUPPORT_EMAIL } from "./lib/constants";
2828
import { ApiKeyPayload, RepositoryQuery } from "./lib/types";
29-
import { withAuthV2, withOptionalAuthV2, withMinimumOrgRole } from "./withAuthV2";
29+
import { withAuthV2, withOptionalAuthV2, withMinimumOrgRole, withAuthV2_skipOrgMembershipCheck } from "./withAuthV2";
3030
import { getBrowsePath } from "./app/[domain]/browse/hooks/utils";
3131

3232
const logger = createLogger('web-actions');
@@ -834,7 +834,7 @@ export const getMe = async () => sew(() =>
834834
}));
835835

836836
export const redeemInvite = async (inviteId: string): Promise<{ success: boolean } | ServiceError> => sew(() =>
837-
withAuthV2(async ({ user, prisma }) => {
837+
withAuthV2_skipOrgMembershipCheck(async ({ user, prisma }) => {
838838
const invite = await prisma.invite.findUnique({
839839
where: {
840840
id: inviteId,
@@ -908,7 +908,7 @@ export const redeemInvite = async (inviteId: string): Promise<{ success: boolean
908908
}));
909909

910910
export const getInviteInfo = async (inviteId: string) => sew(() =>
911-
withAuthV2(async ({ user, prisma }) => {
911+
withAuthV2_skipOrgMembershipCheck(async ({ user, prisma }) => {
912912
const invite = await prisma.invite.findUnique({
913913
where: {
914914
id: inviteId,

packages/web/src/app/invite/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { isServiceError } from "@/lib/utils";
44
import { orgNotFound, ServiceError } from "@/lib/serviceError";
55
import { sew } from "@/actions";
66
import { addUserToOrganization } from "@/lib/authUtils";
7-
import { withAuthV2 } from "@/withAuthV2";
7+
import { withAuthV2_skipOrgMembershipCheck } from "@/withAuthV2";
88
import { StatusCodes } from "http-status-codes";
99
import { ErrorCode } from "@/lib/errorCodes";
1010

1111
export const joinOrganization = async (orgId: number, inviteLinkId?: string) => sew(async () =>
12-
withAuthV2(async ({ user, prisma }) => {
12+
withAuthV2_skipOrgMembershipCheck(async ({ user, prisma }) => {
1313
const org = await prisma.org.findUnique({
1414
where: {
1515
id: orgId,

packages/web/src/withAuthV2.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,41 @@ import { ErrorCode } from "./lib/errorCodes";
1010
import { getOrgMetadata, isServiceError } from "./lib/utils";
1111
import { hasEntitlement } from "@sourcebot/shared";
1212

13-
interface OptionalAuthContext {
13+
type OptionalAuthContext = {
1414
user?: UserWithAccounts;
1515
org: Org;
1616
role: OrgRole;
1717
prisma: PrismaClient;
1818
}
1919

20-
interface RequiredAuthContext {
20+
type RequiredAuthContext = {
2121
user: UserWithAccounts;
2222
org: Org;
2323
role: Exclude<OrgRole, 'GUEST'>;
2424
prisma: PrismaClient;
2525
}
2626

27+
/**
28+
* Requires a logged-in user but does NOT check org membership.
29+
* Use this for actions where the user may not yet be a member
30+
* of the org (e.g. joining an org, redeeming an invite).
31+
*/
32+
export const withAuthV2_skipOrgMembershipCheck = async <T>(fn: (params: Omit<RequiredAuthContext, 'role'> & { role: OrgRole; }) => Promise<T>) => {
33+
const authContext = await getAuthContext();
34+
35+
if (isServiceError(authContext)) {
36+
return authContext;
37+
}
38+
39+
const { user, prisma, org, role } = authContext;
40+
41+
if (!user) {
42+
return notAuthenticated();
43+
}
44+
45+
return fn({ user, prisma, org, role });
46+
};
47+
2748
export const withAuthV2 = async <T>(fn: (params: RequiredAuthContext) => Promise<T>) => {
2849
const authContext = await getAuthContext();
2950

0 commit comments

Comments
 (0)