Skip to content

Commit

Permalink
refactor: Use auth state instead of routes!
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Anton committed May 7, 2024
1 parent 55e899c commit c68d35c
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 25 deletions.
1 change: 0 additions & 1 deletion libs/shared-web/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './routes.enum';
export * from './BaseEnvironmentEnum';
export * from './unprotected-routes.const';
12 changes: 0 additions & 12 deletions libs/shared-web/src/constants/unprotected-routes.const.ts

This file was deleted.

8 changes: 6 additions & 2 deletions libs/shared-web/src/providers/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { useContext } from 'react';
import { IOrganizationEntity, IUserEntity, IJwtPayload } from '@novu/shared';
import { useAuthController, useFeatureFlags } from '../hooks';
import { useAuthController } from '../hooks';

type UserContext = {
token: string | null;
isLoggedIn: boolean;
currentUser: IUserEntity | undefined;
isUserLoading: boolean;
currentOrganization: IOrganizationEntity | undefined;
Expand All @@ -15,6 +16,7 @@ type UserContext = {

const AuthContext = React.createContext<UserContext>({
token: null,
isLoggedIn: false,
currentUser: undefined,
isUserLoading: true,
setToken: undefined as any,
Expand All @@ -27,12 +29,14 @@ const AuthContext = React.createContext<UserContext>({
export const useAuthContext = (): UserContext => useContext(AuthContext);

export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
const { token, setToken, user, organization, isUserLoading, logout, jwtPayload, organizations } = useAuthController();
const { token, setToken, user, organization, isUserLoading, logout, jwtPayload, organizations, isLoggedIn } =
useAuthController();

return (
<AuthContext.Provider
value={{
currentUser: user,
isLoggedIn,
isUserLoading,
currentOrganization: organization,
organizations,
Expand Down
8 changes: 4 additions & 4 deletions libs/shared-web/src/providers/LaunchDarklyProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { asyncWithLDProvider } from 'launchdarkly-react-client-sdk';
import { PropsWithChildren, ReactNode, useEffect, useRef, useState } from 'react';
import { LAUNCH_DARKLY_CLIENT_SIDE_ID } from '../config';
import { useFeatureFlags } from '../hooks';
import { checkIsUnprotectedPathname, checkShouldUseLaunchDarkly } from '../utils';
import { checkShouldUseLaunchDarkly } from '../utils';
import { useAuthContext } from './AuthProvider';

/** A provider with children required */
Expand Down Expand Up @@ -33,7 +33,7 @@ export const LaunchDarklyProvider: React.FC<PropsWithChildren<ILaunchDarklyProvi
if (!authContext) {
throw new Error('LaunchDarklyProvider must be used within <AuthProvider>!');
}
const { currentOrganization } = authContext;
const { currentOrganization, isLoggedIn } = authContext;

useEffect(() => {
// no need to fetch if LD is disabled or there isn't an org to query against
Expand Down Expand Up @@ -61,9 +61,9 @@ export const LaunchDarklyProvider: React.FC<PropsWithChildren<ILaunchDarklyProvi

/**
* For self-hosted, LD will not be enabled, so do not block initialization.
* Checking unprotected (routes without org-based auth) is required to ensure that such routes still load.
* Must not show the fallback if the user isn't logged-in to avoid issues with un-authenticated routes (i.e. login).
*/
if (checkShouldUseLaunchDarkly() && !checkIsUnprotectedPathname(window.location.pathname) && !isLDReady) {
if (checkShouldUseLaunchDarkly() && isLoggedIn && !isLDReady) {
return <>{fallbackDisplay}</>;
}

Expand Down
5 changes: 0 additions & 5 deletions libs/shared-web/src/utils/checkIsUnprotectedPathname.ts

This file was deleted.

1 change: 0 additions & 1 deletion libs/shared-web/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './segment';
export * from './checkIsUnprotectedPathname';
export * from './checkShouldUseLaunchDarkly';

0 comments on commit c68d35c

Please sign in to comment.