Skip to content
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

fix(web): Preserve last active organization across full page reloads #6978

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading