-
Notifications
You must be signed in to change notification settings - Fork 0
fix(auth): fall back to the manual form when the OIDC provider is unreachable #9
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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -12,7 +12,7 @@ import { isValidReturnTo, normalizeReturnTo } from '@documenso/lib/utils/is-vali | |||||
| import { msg } from '@lingui/core/macro'; | ||||||
| import { Trans } from '@lingui/react/macro'; | ||||||
| import { Loader2Icon } from 'lucide-react'; | ||||||
| import { useEffect } from 'react'; | ||||||
| import { useEffect, useState } from 'react'; | ||||||
| import { redirect, useSearchParams } from 'react-router'; | ||||||
|
|
||||||
| import { SignUpForm } from '~/components/forms/signup'; | ||||||
|
|
@@ -78,24 +78,42 @@ export default function SignUp({ loaderData }: Route.ComponentProps) { | |||||
| } = loaderData; | ||||||
|
|
||||||
| const [searchParams] = useSearchParams(); | ||||||
| const [isRedirectFailed, setIsRedirectFailed] = useState(false); | ||||||
| const [isEmbeddedRedirect, setIsEmbeddedRedirect] = useState(false); | ||||||
|
|
||||||
| // Suppress the automatic redirect when the user asked for the manual form | ||||||
| // via ?direct=1, or when a previous OIDC attempt bounced back with an error | ||||||
| // (avoids a redirect loop). | ||||||
| const isDirectEntry = searchParams.get('direct') === '1'; | ||||||
| const hasIdpError = searchParams.get('error') !== null; | ||||||
|
|
||||||
| const shouldRedirectToOIDC = shouldAutoRedirectToOIDC && !isDirectEntry && !hasIdpError; | ||||||
| useEffect(() => { | ||||||
| const params = new URLSearchParams(window.location.hash.slice(1)); | ||||||
|
|
||||||
| setIsEmbeddedRedirect(params.get('embedded') === 'true'); | ||||||
| }, []); | ||||||
|
|
||||||
| const shouldRedirectToOIDC = shouldAutoRedirectToOIDC && !isDirectEntry && !hasIdpError && !isEmbeddedRedirect; | ||||||
|
|
||||||
| useEffect(() => { | ||||||
| if (!shouldRedirectToOIDC) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| void authClient.oidc.signIn({ redirectPath: returnTo ?? '/' }); | ||||||
| // Guard against the initial render racing the embedded detection above: | ||||||
| // read the hash synchronously so embedded contexts never bounce to the IdP. | ||||||
| if (new URLSearchParams(window.location.hash.slice(1)).get('embedded') === 'true') { | ||||||
| return; | ||||||
| } | ||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||
|
|
||||||
| authClient.oidc.signIn({ redirectPath: returnTo ?? '/' }).catch(() => { | ||||||
| // Fall back to the manual form instead of leaving the user on the | ||||||
| // spinner forever when the IdP is unreachable. | ||||||
| setIsRedirectFailed(true); | ||||||
| }); | ||||||
| }, [shouldRedirectToOIDC, returnTo]); | ||||||
|
|
||||||
| if (shouldRedirectToOIDC) { | ||||||
| if (shouldRedirectToOIDC && !isRedirectFailed) { | ||||||
|
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. Since
Suggested change
|
||||||
| return ( | ||||||
| <div className="w-screen max-w-lg px-4"> | ||||||
| <div className="flex flex-col items-center justify-center gap-y-4 py-12"> | ||||||
|
|
||||||
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.
To prevent users in embedded contexts from getting stuck on the spinner forever, we need to track whether the signup is embedded. Let's add the
isEmbeddedRedirectstate and its correspondinguseEffectto detect the#embedded=truehash on mount, matching the implementation insignin.tsx.