Skip to content

Commit

Permalink
fix(web): Preserve last active organization across full page reloads (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SokratisVidros authored Nov 13, 2024
1 parent c7047ef commit 0ba509a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 35 deletions.
22 changes: 6 additions & 16 deletions apps/dashboard/src/context/auth/auth-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReactNode, useCallback, useEffect, useMemo } from 'react';
import { useAuth, useOrganization, useOrganizationList, useUser } from '@clerk/clerk-react';
import { useAuth, useOrganization, useUser } from '@clerk/clerk-react';
import type { UserResource } from '@clerk/types';
import { ROUTES } from '@/utils/routes';
import type { AuthContextValue } from './types';
Expand All @@ -10,7 +10,6 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
const { orgId } = useAuth();
const { user: clerkUser, isLoaded: isUserLoaded } = useUser();
const { organization: clerkOrganization, isLoaded: isOrganizationLoaded } = useOrganization();
const { setActive, isLoaded: isOrgListLoaded } = useOrganizationList({ userMemberships: { infinite: true } });

const redirectTo = useCallback(
({
Expand Down Expand Up @@ -44,23 +43,14 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
[]
);

// check if user has active organization
useEffect(() => {
if (orgId) {
return;
}

if (isOrgListLoaded && clerkUser) {
const hasOrgs = clerkUser.organizationMemberships.length > 0;
if (!clerkUser || orgId) return;

if (hasOrgs) {
const firstOrg = clerkUser.organizationMemberships[0].organization;
setActive({ organization: firstOrg });
} else if (!window.location.href.includes(ROUTES.SIGNUP_ORGANIZATION_LIST)) {
redirectTo({ url: ROUTES.SIGNUP_ORGANIZATION_LIST });
}
const hasOrganizations = clerkUser.organizationMemberships.length > 0;
if (!hasOrganizations && window.location.pathname !== ROUTES.SIGNUP_ORGANIZATION_LIST) {
return redirectTo({ url: ROUTES.SIGNUP_ORGANIZATION_LIST });
}
}, [setActive, isOrgListLoaded, clerkUser, orgId, redirectTo]);
}, [clerkUser, orgId, redirectTo]);

const currentUser = useMemo(() => (clerkUser ? toUserEntity(clerkUser as UserResource) : undefined), [clerkUser]);
const currentOrganization = useMemo(
Expand Down
27 changes: 8 additions & 19 deletions apps/web/src/ee/clerk/providers/EnterpriseAuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, useCallback, useEffect, useState, useMemo } from 'react';
import type { IOrganizationEntity, IUserEntity, ProductUseCases } from '@novu/shared';
import { useAuth, useUser, useOrganization, useOrganizationList } from '@clerk/clerk-react';
import { createContext, useCallback, useEffect, useMemo } from 'react';
import type { IOrganizationEntity, IUserEntity } from '@novu/shared';
import { useAuth, useUser, useOrganization } from '@clerk/clerk-react';
import { OrganizationResource, UserResource } from '@clerk/types';

import { useNavigate } from 'react-router-dom';
Expand All @@ -20,8 +20,6 @@ export const EnterpriseAuthProvider = ({ children }: { children: React.ReactNode
const { signOut, orgId } = useAuth();
const { user: clerkUser, isLoaded: isUserLoaded } = useUser();
const { organization: clerkOrganization, isLoaded: isOrganizationLoaded } = useOrganization();
// TODO @ChmaraX: Can we use setActive from useSession, useSignIn, or useSignUp to avoid loading the list?
const { setActive, isLoaded: isOrgListLoaded } = useOrganizationList({ userMemberships: { infinite: true } });

const segment = useSegment();
const queryClient = useQueryClient();
Expand Down Expand Up @@ -90,23 +88,14 @@ export const EnterpriseAuthProvider = ({ children }: { children: React.ReactNode
return {};
}, [clerkOrganization]);

// check if user has active organization
useEffect(() => {
if (orgId) {
return;
}

if (isOrgListLoaded && clerkUser) {
const hasOrgs = clerkUser.organizationMemberships.length > 0;
if (!clerkUser || orgId) return;

if (hasOrgs) {
const firstOrg = clerkUser.organizationMemberships[0].organization;
setActive({ organization: firstOrg });
} else if (!window.location.href.includes(ROUTES.AUTH_SIGNUP_ORGANIZATION_LIST)) {
redirectTo({ url: ROUTES.AUTH_SIGNUP_ORGANIZATION_LIST });
}
const hasOrganizations = clerkUser.organizationMemberships.length > 0;
if (!hasOrganizations && window.location.pathname !== ROUTES.AUTH_SIGNUP_ORGANIZATION_LIST) {
return redirectTo({ url: ROUTES.AUTH_SIGNUP_ORGANIZATION_LIST });
}
}, [setActive, isOrgListLoaded, clerkUser, orgId, redirectTo]);
}, [clerkUser, orgId, redirectTo]);

const currentUser = useMemo(() => (clerkUser ? toUserEntity(clerkUser) : undefined), [clerkUser]);
const currentOrganization = useMemo(
Expand Down

0 comments on commit 0ba509a

Please sign in to comment.