-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
refactor(web): remove jwt-decode and refactor auth logic #5620
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,33 +2,23 @@ import * as Sentry from '@sentry/react'; | |
|
||
import { IOrganizationEntity } from '@novu/shared'; | ||
import { asyncWithLDProvider } from 'launchdarkly-react-client-sdk'; | ||
import { PropsWithChildren, ReactNode, useEffect, useMemo, useRef, useState } from 'react'; | ||
import { PropsWithChildren, useEffect, useMemo, useRef, useState } from 'react'; | ||
import { useFeatureFlags, useAuthContext, LAUNCH_DARKLY_CLIENT_SIDE_ID } from '@novu/shared-web'; | ||
import { selectShouldInitializeLaunchDarkly } from './utils/selectShouldInitializeLaunchDarkly'; | ||
import { selectShouldShowLaunchDarklyFallback } from './utils/selectShouldShowLaunchDarklyFallback'; | ||
|
||
/** A provider with children required */ | ||
type GenericLDProvider = Awaited<ReturnType<typeof asyncWithLDProvider>>; | ||
|
||
/** Simply renders the children */ | ||
const DEFAULT_GENERIC_PROVIDER: GenericLDProvider = ({ children }) => <>{children}</>; | ||
|
||
export interface ILaunchDarklyProviderProps { | ||
/** Renders when LaunchDarkly is enabled and is awaiting initialization */ | ||
fallbackDisplay: ReactNode; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed, this is moved to the |
||
/** | ||
* Async provider for feature flags. | ||
* | ||
* @requires AuthProvider must be wrapped in the AuthProvider. | ||
*/ | ||
export const LaunchDarklyProvider: React.FC<PropsWithChildren<ILaunchDarklyProviderProps>> = ({ | ||
children, | ||
fallbackDisplay, | ||
}) => { | ||
export const LaunchDarklyProvider: React.FC<PropsWithChildren<{}>> = ({ children }) => { | ||
const LDProvider = useRef<GenericLDProvider>(DEFAULT_GENERIC_PROVIDER); | ||
const [isLDReady, setIsLDReady] = useState<boolean>(false); | ||
|
||
const authContext = useAuthContext(); | ||
if (!authContext) { | ||
|
@@ -68,21 +58,11 @@ export const LaunchDarklyProvider: React.FC<PropsWithChildren<ILaunchDarklyProvi | |
}); | ||
} catch (err: unknown) { | ||
Sentry.captureException(err); | ||
} finally { | ||
setIsLDReady(true); | ||
} | ||
}; | ||
|
||
fetchLDProvider(); | ||
}, [setIsLDReady, shouldInitializeLd, currentOrganization]); | ||
|
||
/** | ||
* For self-hosted, LD will not be enabled, so do not block initialization. | ||
* Must not show the fallback if the user isn't logged-in to avoid issues with un-authenticated routes (i.e. login). | ||
*/ | ||
if (selectShouldShowLaunchDarklyFallback(authContext, isLDReady)) { | ||
return <>{fallbackDisplay}</>; | ||
} | ||
}, [shouldInitializeLd, currentOrganization]); | ||
|
||
return ( | ||
<LDProvider.current> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { selectHasUserCompletedSignUp, UserContext } from '@novu/shared-web'; | ||
import { UserContext } from '@novu/shared-web'; | ||
import { checkShouldUseLaunchDarkly } from '@novu/shared-web'; | ||
|
||
/** Determine if LaunchDarkly should be initialized based on the current auth context */ | ||
export function selectShouldInitializeLaunchDarkly(userCtx: UserContext): boolean { | ||
const { isLoggedIn, currentOrganization } = userCtx; | ||
const { isLoggedIn, currentUser, currentOrganization } = userCtx; | ||
// don't show fallback if LaunchDarkly isn't enabled | ||
if (!checkShouldUseLaunchDarkly()) { | ||
return false; | ||
|
@@ -22,7 +22,7 @@ export function selectShouldInitializeLaunchDarkly(userCtx: UserContext): boolea | |
* have an organizationId yet that we can use for org-based feature flags. To prevent from blocking this page | ||
* from loading during this "limbo" state, we should initialize LD with the anonymous context. | ||
*/ | ||
if (!selectHasUserCompletedSignUp(userCtx)) { | ||
if (!currentUser?.organizationId) { | ||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Available through user context now. |
||
} | ||
|
||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Navigate, useLocation } from 'react-router-dom'; | ||
import { ROUTES } from '../../constants/routes.enum'; | ||
import { useBlueprint, useAuthController } from '../../hooks/index'; | ||
|
||
export function EnsureOnboardingComplete({ children }: any) { | ||
useBlueprint(); | ||
const location = useLocation(); | ||
const { user } = useAuthController(); | ||
|
||
if ((!user?.organizationId || !user?.environmentId) && location.pathname !== ROUTES.AUTH_APPLICATION) { | ||
return <Navigate to={ROUTES.AUTH_APPLICATION} replace />; | ||
} else { | ||
return children; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file's contents came from |
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,3 +1,3 @@ | ||||||
import { useAuthController, applyToken, getTokenPayload, getToken } from '@novu/shared-web'; | ||||||
import { useAuthController, applyToken } from '@novu/shared-web'; | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a bunch of these |
||||||
export { useAuthController, applyToken, getTokenPayload, getToken }; | ||||||
export { useAuthController, applyToken }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { useNavigate, useLocation, useSearchParams } from 'react-router-dom'; | ||
import { useEffect } from 'react'; | ||
|
||
import { getToken } from './useAuthController'; | ||
import { useAuthController } from './useAuthController'; | ||
import { useSegment } from '../components/providers/SegmentProvider'; | ||
import { ROUTES } from '../constants/routes.enum'; | ||
|
||
|
@@ -12,10 +12,10 @@ export const useBlueprint = () => { | |
const { pathname } = useLocation(); | ||
const segment = useSegment(); | ||
const id = localStorage.getItem('blueprintId'); | ||
const token = getToken(); | ||
const { token } = useAuthController(); | ||
|
||
useEffect(() => { | ||
if (id && token !== null) { | ||
if (id && !!token) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From here on, we'll see several files be updated to pull from the auth controller, this ensures only the auth controller deals with surfacing auth concerns as the source of truth. |
||
navigate(ROUTES.WORKFLOWS_CREATE, { | ||
replace: true, | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import axios from 'axios'; | ||
import { useCallback } from 'react'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
@@ -11,11 +10,10 @@ import { vercelIntegrationSetup } from '../api/vercel-integration'; | |
export function useVercelIntegration() { | ||
const { token } = useAuthContext(); | ||
const isLoggedIn = !!token; | ||
const isAxiosAuthorized = axios.defaults.headers.common.Authorization; | ||
|
||
const { code, next, configurationId } = useVercelParams(); | ||
|
||
const canStartSetup = Boolean(code && next && isLoggedIn && isAxiosAuthorized); | ||
const canStartSetup = Boolean(code && next && isLoggedIn); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const navigate = useNavigate(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import { library } from '@fortawesome/fontawesome-svg-core'; | ||
import { far } from '@fortawesome/free-regular-svg-icons'; | ||
import { fas } from '@fortawesome/free-solid-svg-icons'; | ||
import { applyToken, getToken } from '@novu/shared-web'; | ||
import * as Sentry from '@sentry/react'; | ||
import { Integrations } from '@sentry/tracing'; | ||
import { ENV, SENTRY_DSN } from './config'; | ||
|
@@ -57,8 +56,4 @@ export const initializeApp = () => { | |
}, | ||
}); | ||
} | ||
|
||
const tokenStoredToken: string = getToken(); | ||
|
||
applyToken(tokenStoredToken); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are no longer using the |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
import { useEffect } from 'react'; | ||
import { useNavigate, useSearchParams } from 'react-router-dom'; | ||
import jwtDecode from 'jwt-decode'; | ||
import { IJwtPayload } from '@novu/shared'; | ||
|
||
import { useAuthContext } from '../../components/providers/AuthProvider'; | ||
import { LoginForm } from './components/LoginForm'; | ||
|
@@ -15,7 +13,7 @@ import { ROUTES } from '../../constants/routes.enum'; | |
|
||
export default function LoginPage() { | ||
useBlueprint(); | ||
const { setToken, token: oldToken } = useAuthContext(); | ||
const { setToken, token: oldToken, currentUser } = useAuthContext(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decoded jwt params are available on |
||
const segment = useSegment(); | ||
const navigate = useNavigate(); | ||
const [params] = useSearchParams(); | ||
|
@@ -31,9 +29,7 @@ export default function LoginPage() { | |
|
||
useEffect(() => { | ||
if (token) { | ||
const user = jwtDecode<IJwtPayload>(token); | ||
|
||
if (!invitationToken && (!user.organizationId || !user.environmentId)) { | ||
if (!invitationToken && currentUser?._id && (!currentUser?.organizationId || !currentUser?.environmentId)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The additional check here for |
||
const authApplicationLink = isFromVercel | ||
? `${ROUTES.AUTH_APPLICATION}?code=${code}&next=${next}` | ||
: ROUTES.AUTH_APPLICATION; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Temporarily adding this to fix the broken
next
.