sign-in-mfa.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // @ts-nocheck
  2. import * as Sentry from '@sentry/nextjs'
  3. import { useQueryClient } from '@tanstack/react-query'
  4. import { getAccessToken, useParams } from 'common'
  5. import { useRouter } from 'next/router'
  6. import { useEffect, useState } from 'react'
  7. import { toast } from 'sonner'
  8. import { LogoLoader } from 'ui'
  9. import { SignInMfaForm } from '@/components/interfaces/SignIn/SignInMfaForm'
  10. import SignInLayout from '@/components/layouts/SignInLayout/SignInLayout'
  11. import { useAddLoginEvent } from '@/data/misc/audit-login-mutation'
  12. import { useSendEventMutation } from '@/data/telemetry/send-event-mutation'
  13. import useLatest from '@/hooks/misc/useLatest'
  14. import { auth, buildPathWithParams, getReturnToPath } from '@/lib/gotrue'
  15. import type { NextPageWithLayout } from '@/types'
  16. const SignInMfaPage: NextPageWithLayout = () => {
  17. const router = useRouter()
  18. const queryClient = useQueryClient()
  19. const {
  20. // current methods for mfa are github and sso
  21. method: signInMethod = 'unknown',
  22. } = useParams()
  23. const signInMethodRef = useLatest(signInMethod)
  24. const { mutate: sendEvent } = useSendEventMutation()
  25. const { mutate: addLoginEvent } = useAddLoginEvent()
  26. const [loading, setLoading] = useState(true)
  27. // This useEffect redirects the user to MFA if they're already halfway signed in
  28. useEffect(() => {
  29. auth
  30. .initialize()
  31. .then(async ({ error }) => {
  32. if (error) {
  33. // if there was a problem signing in via the url, don't redirect
  34. setLoading(false)
  35. return
  36. }
  37. const token = await getAccessToken()
  38. if (token) {
  39. const { data, error } = await auth.mfa.getAuthenticatorAssuranceLevel()
  40. if (error) {
  41. // if there was a problem signing in via the url, don't redirect
  42. toast.error(
  43. `Failed to retrieve assurance level: ${error.message}. Please try signing in again`
  44. )
  45. setLoading(false)
  46. return router.push({ pathname: '/sign-in', query: router.query })
  47. }
  48. if (data.currentLevel === data.nextLevel) {
  49. sendEvent({
  50. action: 'sign_in',
  51. properties: {
  52. category: 'account',
  53. method: signInMethodRef.current,
  54. },
  55. })
  56. addLoginEvent({})
  57. await queryClient.resetQueries()
  58. router.push(getReturnToPath())
  59. return
  60. } else {
  61. // Show the MFA form
  62. setLoading(false)
  63. return
  64. }
  65. } else {
  66. // if the user doesn't have a token, he needs to go back to the sign-in page
  67. const redirectTo = buildPathWithParams('/sign-in')
  68. router.replace(redirectTo)
  69. return
  70. }
  71. })
  72. .catch((error) => {
  73. Sentry.captureException(error)
  74. console.error('Auth initialization error:', error)
  75. toast.error('Failed to initialize authentication. Please try again.')
  76. setLoading(false)
  77. router.push({ pathname: '/sign-in', query: router.query })
  78. })
  79. }, [])
  80. if (loading) {
  81. return (
  82. <div className="flex flex-col flex-1 bg-alternative h-screen items-center justify-center">
  83. <LogoLoader />
  84. </div>
  85. )
  86. }
  87. return (
  88. <SignInLayout
  89. heading="Two-factor authentication"
  90. subheading="Enter the authentication code from your two-factor authentication app"
  91. logoLinkToMarketingSite={true}
  92. >
  93. <div className="flex flex-col gap-5">
  94. <SignInMfaForm />
  95. </div>
  96. </SignInLayout>
  97. )
  98. }
  99. export default SignInMfaPage