11import { startAuthentication } from '@simplewebauthn/browser'
22import { ArrowLeftIcon , KeyIcon } from 'lucide-react'
33import { useRouter , useSearchParams } from 'next/navigation'
4- import { useState } from 'react'
4+ import { useEffect , useState } from 'react'
55import { toast } from 'sonner'
66
77import { Button } from '../ui/button'
@@ -13,6 +13,7 @@ import { useVerifyMfa } from '@/src/api/hooks/useVerifyMfa'
1313import { instance } from '@/src/api/instance'
1414import { generateAuthenticationOptions } from '@/src/api/requests'
1515import { MFA_OPTIONS , MfaMethod , ROUTES } from '@/src/constants'
16+ import { analytics } from '@/src/lib/analytics'
1617import { cookies } from '@/src/lib/cookie'
1718import { cn } from '@/src/lib/utils'
1819
@@ -32,7 +33,11 @@ export function MfaForm({ ticket, methods, userId, onBack }: MfaFormProps) {
3233 const searchParams = useSearchParams ( )
3334
3435 const { mutate, isPending } = useVerifyMfa ( {
35- onSuccess ( data ) {
36+ onSuccess ( data , variables ) {
37+ console . log ( 'METHOD: ' , variables . method )
38+
39+ analytics . auth . mfa . success ( variables . method ! )
40+
3641 cookies . set ( 'token' , data . token , { expires : 30 } )
3742
3843 instance . defaults . headers [ 'X-Session-Token' ] = data . token
@@ -42,16 +47,24 @@ export function MfaForm({ ticket, methods, userId, onBack }: MfaFormProps) {
4247
4348 router . push ( redirectTo )
4449 } ,
45- onError ( error : any ) {
46- toast . error ( error . response ?. data ?. message ?? 'Ошибка при входе' )
50+ onError ( error : any , variables ) {
51+ const message = error . response ?. data ?. message ?? 'Ошибка при входе'
52+ analytics . auth . mfa . fail ( variables . method ! , message )
53+
54+ toast . error ( message )
4755 }
4856 } )
4957
58+ useEffect ( ( ) => {
59+ analytics . auth . mfa . methodsShown ( methods )
60+ } , [ methods ] )
61+
5062 const availableOptions = MFA_OPTIONS . filter ( option =>
5163 methods . includes ( option . id )
5264 )
53-
5465 const handleMethodSelect = ( method : MfaMethod ) => {
66+ analytics . auth . mfa . select ( method )
67+
5568 setSelectedMethod ( method )
5669 setCode ( '' )
5770 }
@@ -68,6 +81,8 @@ export function MfaForm({ ticket, methods, userId, onBack }: MfaFormProps) {
6881 const handleVerify = ( ) => {
6982 if ( ! selectedMethod || ! code . trim ( ) ) return
7083
84+ analytics . auth . mfa . submit ( selectedMethod )
85+
7186 const payload =
7287 selectedMethod === 'totp'
7388 ? { ticket, totpCode : code }
@@ -77,14 +92,17 @@ export function MfaForm({ ticket, methods, userId, onBack }: MfaFormProps) {
7792 }
7893
7994 const handlePasskeyAuth = async ( ) => {
95+ analytics . auth . mfa . passkeyStart ( )
8096 setIsLoading ( true )
8197 try {
8298 const options = await generateAuthenticationOptions ( { userId } )
83-
8499 const attestationResponse = await startAuthentication ( options )
85100
101+ analytics . auth . mfa . passkeySuccess ( )
102+
86103 mutate ( { ticket, attestationResponse } )
87- } catch ( error ) {
104+ } catch ( error : any ) {
105+ analytics . auth . mfa . passkeyFail ( error ?. message )
88106 console . error ( 'Passkey authentication failed:' , error )
89107 } finally {
90108 setIsLoading ( false )
0 commit comments