Files
supabase/apps/studio/hooks/misc/withAuth.tsx
T
df52ea7ee0 feat: Replace all toasts with sonner (#28250)
* Update the design of the sonner toasts. Add the close button by default.

* Migrate studio and www apps to use the SonnerToaster.

* Migrate all toasts from studio.

* Migrate all leftover toasts in studio.

* Add a new toast component with progress. Use it in studio.

* Migrate the design-system app.

* Refactor the consent toast to use sonner.

* Switch docs to use the new sonner toasts.

* Remove toast examples from the design-system app.

* Remove all toast-related components and old code.

* Fix the progress bar in the toast progress component. Also make the bottom components vertically centered.

* Fix the width of the toast progress.

* Use text-foreground-lighter instead of muted for ToastProgress text

* Rename ToastProgress to SonnerProgress.

* Shorten the text in sonner progress.

* Use the correct classes for the close button. Add a const var for the default toast duration. Remove the custom width class from sonner.

* Set the position for all progress toasts to bottom right. Set the duration for all toasts to the default (when reusing a toast id from loading/progress toast, the duration is set to infinity).

* Fix the playwright tests.

* Refactor imports to use ui instead of @ui.

* Change all imports of react-hot-toast with sonner. These components were merged since the last commit to this branch.

* Remove react-hot-toast lib.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
2024-08-31 07:50:51 +08:00

83 lines
2.9 KiB
TypeScript

import { useRouter } from 'next/router'
import { ComponentType, useEffect } from 'react'
import { toast } from 'sonner'
import { usePermissionsQuery } from 'data/permissions/permissions-query'
import { useAuthenticatorAssuranceLevelQuery } from 'data/profile/mfa-authenticator-assurance-level-query'
import { useAuth } from 'lib/auth'
import { IS_PLATFORM } from 'lib/constants'
import { NextPageWithLayout, isNextPageWithLayout } from 'types'
export function withAuth<T>(
WrappedComponent: ComponentType<T> | NextPageWithLayout<T, T>,
options: {
/**
* The auth level used to check the user credentials. In most cases, if the user has MFA enabled
* we want the highest level (which is 2) for all pages. For certain pages, the user should be
* able to access them even if he didn't finished his login (typed in his MFA code), for example
* the support page: We want the user to be able to submit a ticket even if he's not fully
* signed in.
* @default true
*/
useHighestAAL: boolean
} = { useHighestAAL: true }
) {
// ignore auth in self-hosted
if (!IS_PLATFORM) {
return WrappedComponent
}
const WithAuthHOC: ComponentType<T> = (props) => {
const router = useRouter()
const { isLoading, session } = useAuth()
const { isLoading: isAALLoading, data: aalData } = useAuthenticatorAssuranceLevelQuery({
onError(error) {
toast.error(
`Failed to fetch authenticator assurance level: ${error.message}. Try refreshing your browser, or reach out to us via a support ticket if the issue persists`
)
},
})
usePermissionsQuery({
onError(error: any) {
toast.error(
`Failed to fetch permissions: ${error.message}. Try refreshing your browser, or reach out to us via a support ticket if the issue persists`
)
},
})
const isLoggedIn = Boolean(session)
const isFinishedLoading = !isLoading && !isAALLoading
useEffect(() => {
const isCorrectLevel = options.useHighestAAL
? aalData?.currentLevel === aalData?.nextLevel
: true
if (isFinishedLoading && (!isLoggedIn || !isCorrectLevel)) {
const searchParams = new URLSearchParams(location.search)
let pathname = location.pathname
if (process.env.NEXT_PUBLIC_BASE_PATH) {
pathname = pathname.replace(process.env.NEXT_PUBLIC_BASE_PATH, '')
}
searchParams.set('returnTo', pathname)
router.push(`/sign-in?${searchParams.toString()}`)
}
}, [session, isLoading, router, aalData, isFinishedLoading, isLoggedIn])
const InnerComponent = WrappedComponent as any
return <InnerComponent {...props} />
}
WithAuthHOC.displayName = `withAuth(${WrappedComponent.displayName})`
if (isNextPageWithLayout(WrappedComponent)) {
;(WithAuthHOC as NextPageWithLayout<T, T>).getLayout = WrappedComponent.getLayout
}
return WithAuthHOC
}