forgot-password-mfa.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import * as Sentry from '@sentry/nextjs'
  2. import { useQueryClient } from '@tanstack/react-query'
  3. import { getAccessToken } from 'common'
  4. import { useRouter } from 'next/router'
  5. import { useEffect, useState } from 'react'
  6. import { toast } from 'sonner'
  7. import { LogoLoader } from 'ui'
  8. import { SignInMfaForm } from '@/components/interfaces/SignIn/SignInMfaForm'
  9. import ForgotPasswordLayout from '@/components/layouts/SignInLayout/ForgotPasswordLayout'
  10. import { auth, buildPathWithParams, getReturnToPath } from '@/lib/gotrue'
  11. import type { NextPageWithLayout } from '@/types'
  12. const ForgotPasswordMfa: NextPageWithLayout = () => {
  13. const router = useRouter()
  14. const queryClient = useQueryClient()
  15. const [loading, setLoading] = useState(true)
  16. // This useEffect redirects the user to MFA if they're already halfway signed in
  17. useEffect(() => {
  18. auth
  19. .initialize()
  20. .then(async ({ error }) => {
  21. if (error) {
  22. // if there was a problem signing in via the url, don't redirect
  23. setLoading(false)
  24. return
  25. }
  26. const token = await getAccessToken()
  27. if (token) {
  28. const { data, error } = await auth.mfa.getAuthenticatorAssuranceLevel()
  29. if (error) {
  30. // if there was a problem signing in via the url, don't redirect
  31. toast.error(
  32. `Failed to retrieve assurance level: ${error.message}. Please try signing in again`
  33. )
  34. setLoading(false)
  35. return router.push({ pathname: '/sign-in', query: router.query })
  36. }
  37. if (data.currentLevel === data.nextLevel) {
  38. await queryClient.resetQueries()
  39. router.push(getReturnToPath())
  40. return
  41. } else {
  42. // Show the MFA form
  43. setLoading(false)
  44. return
  45. }
  46. } else {
  47. // if the user doesn't have a token, he needs to go back to the sign-in page
  48. const redirectTo = buildPathWithParams('/sign-in')
  49. router.replace(redirectTo)
  50. return
  51. }
  52. })
  53. .catch((error) => {
  54. Sentry.captureException(error)
  55. console.error('Auth initialization error:', error)
  56. toast.error('Failed to initialize authentication. Please try again.')
  57. setLoading(false)
  58. router.push({ pathname: '/sign-in', query: router.query })
  59. })
  60. }, [])
  61. if (loading) {
  62. return (
  63. <div className="flex flex-col flex-1 bg-alternative h-screen items-center justify-center">
  64. <LogoLoader />
  65. </div>
  66. )
  67. }
  68. return (
  69. <ForgotPasswordLayout
  70. heading="Complete two-factor authentication"
  71. subheading="Enter the authentication code from your two-factor authentication app before changing your password"
  72. >
  73. <SignInMfaForm context="forgot-password" />
  74. </ForgotPasswordLayout>
  75. )
  76. }
  77. export default ForgotPasswordMfa