Skip to content

Commit

Permalink
fix: if user doesn't have an org created, send to create org, not swi…
Browse files Browse the repository at this point in the history
…tcher
  • Loading branch information
mainawycliffe committed Nov 23, 2023
1 parent 735859b commit a2989f3
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/components/Authentication/Clerk/ClerkAuthSessionChecker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { RedirectToSignIn, useOrganization, useSession } from "@clerk/nextjs";
import {
RedirectToSignIn,
useOrganization,
useOrganizationList,
useSession
} from "@clerk/nextjs";
import { useRouter } from "next/router";
import { useEffect } from "react";
import FullPageSkeletonLoader from "../../SkeletonLoader/FullPageSkeletonLoader";
Expand All @@ -19,13 +24,33 @@ export default function ClerkAuthSessionChecker({ children }: Props) {
const { isSignedIn, isLoaded: isSessionLoaded } = useSession();
const { isLoaded: isOrganizationLoaded, organization } = useOrganization();

// we need to check if the user has an organization and redirect them to the
// create organization page if they do not
const { isLoaded: isOrganizationListLoaded, userMemberships } =
useOrganizationList();

const { push } = useRouter();

useEffect(() => {
if (isOrganizationLoaded && !organization) {
if (isOrganizationLoaded && isOrganizationListLoaded && !organization) {
// we should redirect to the create organization page if the user is
// signed in and does not have an organization
if (userMemberships?.count === 0) {
push(clerkUrls.createOrganization);
return;
}
// otherwise, we should redirect to the organization switcher page
// may be in the future, we should just display the organization switcher
// as a modal instead of redirecting
push(clerkUrls.organizationSwitcher);
}
}, [isOrganizationLoaded, organization, push]);
}, [
isOrganizationListLoaded,
isOrganizationLoaded,
organization,
push,
userMemberships?.count
]);

if (!isOrganizationLoaded || !isSessionLoaded) {
return <FullPageSkeletonLoader />;
Expand Down

0 comments on commit a2989f3

Please sign in to comment.