({
hasEmailService,
isProduction,
isEmailVerificationEnabled,
}: UseVerificationParams)
| 40 | } |
| 41 | |
| 42 | export function useVerification({ |
| 43 | hasEmailService, |
| 44 | isProduction, |
| 45 | isEmailVerificationEnabled, |
| 46 | }: UseVerificationParams): UseVerificationReturn { |
| 47 | const router = useRouter() |
| 48 | const searchParams = useSearchParams() |
| 49 | const { refetch: refetchSession } = useSession() |
| 50 | const [otp, setOtp] = useState('') |
| 51 | const [email, setEmail] = useState('') |
| 52 | const [status, setStatus] = useState<VerificationStatus>('idle') |
| 53 | const [isResending, setIsResending] = useState(false) |
| 54 | const [isSendingInitialOtp, setIsSendingInitialOtp] = useState(false) |
| 55 | const [errorMessage, setErrorMessage] = useState('') |
| 56 | const [redirectUrl, setRedirectUrl] = useState<string | null>(null) |
| 57 | const [isInviteFlow, setIsInviteFlow] = useState(false) |
| 58 | |
| 59 | useEffect(() => { |
| 60 | if (typeof window !== 'undefined') { |
| 61 | const storedEmail = sessionStorage.getItem('verificationEmail') |
| 62 | if (storedEmail) { |
| 63 | setEmail(storedEmail) |
| 64 | } |
| 65 | |
| 66 | const storedRedirectUrl = sessionStorage.getItem('inviteRedirectUrl') |
| 67 | if (storedRedirectUrl && validateCallbackUrl(storedRedirectUrl)) { |
| 68 | setRedirectUrl(storedRedirectUrl) |
| 69 | } else if (storedRedirectUrl) { |
| 70 | logger.warn('Ignoring unsafe stored invite redirect URL', { url: storedRedirectUrl }) |
| 71 | sessionStorage.removeItem('inviteRedirectUrl') |
| 72 | } |
| 73 | |
| 74 | const storedIsInviteFlow = sessionStorage.getItem('isInviteFlow') |
| 75 | if (storedIsInviteFlow === 'true') { |
| 76 | setIsInviteFlow(true) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | const redirectParam = searchParams.get('redirectAfter') |
| 81 | if (redirectParam) { |
| 82 | if (validateCallbackUrl(redirectParam)) { |
| 83 | setRedirectUrl(redirectParam) |
| 84 | } else { |
| 85 | logger.warn('Ignoring unsafe redirectAfter parameter', { url: redirectParam }) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | const inviteFlowParam = searchParams.get('invite_flow') |
| 90 | if (inviteFlowParam === 'true') { |
| 91 | setIsInviteFlow(true) |
| 92 | } |
| 93 | }, [searchParams]) |
| 94 | |
| 95 | useEffect(() => { |
| 96 | if (email && !isSendingInitialOtp && hasEmailService) { |
| 97 | setIsSendingInitialOtp(true) |
| 98 | } |
| 99 | }, [email, isSendingInitialOtp, hasEmailService]) |
no test coverage detected