import { useRef, useState } from 'react'; import { AuthenticationCard } from '../AuthenticationCard'; import { ArrowLeft, Edit2, Eye, EyeSlash } from 'iconsax-react'; import { Box, Button, IconButton, Stack, TextField, Typography, } from '@mui/material'; import { useTranslation } from 'react-i18next'; import { Link } from 'react-router-dom'; import type { AuthType } from '../../types/authTypes'; import type { CountryCode, GUID } from '@/types/commonTypes'; import { loginWithPassword, sendEmailOtp, sendSmsOtp, } from '../../api/authorizationAPI'; import type { LoginResult, PasswordLoginRequest } from '../../types/userTypes'; import { Icon, useToast } from '@rkheftan/harmony-ui'; import { useApi } from '@/hooks/useApi'; import { useAuth } from '@/hooks/useAuth'; import { generateTokenWithPassword } from '../../api/identityAPI'; export interface EnterPasswordFormProps { onEditValue: () => void; onLoginWithOTP: () => void; onLoggedIn: (loginResult: LoginResult) => void; emailOrPhone: string; authType: AuthType; loginRegisterValue: string; countryCode: CountryCode; authReturnUrl: string; } export const EnterPasswordForm = ({ onEditValue, onLoginWithOTP, onLoggedIn, emailOrPhone, authType, loginRegisterValue, countryCode, authReturnUrl, }: EnterPasswordFormProps) => { const { t } = useTranslation('authentication'); const [passValue, setPassValue] = useState(''); const [inputTouched, setInputTouched] = useState(false); const [showPassword, setShowPassword] = useState(false); const inputRef = useRef(null); const toast = useToast(); const { loading: smsResendLoading, execute: smsResendCall } = useApi(sendSmsOtp); const { loading: emailResendLoading, execute: emailResendCall } = useApi(sendEmailOtp); const { loading: loginWithPassLoading, execute: loginWithPassCall } = useApi(loginWithPassword); const auth = useAuth(); const handleBlur = () => { setInputTouched(true); }; const handleSubmit = async () => { if (!passValue) { inputRef.current?.focus(); } else { const apiRequest: PasswordLoginRequest = { phoneNumber: authType === 'phone' ? countryCode + loginRegisterValue : undefined, email: authType === 'email' ? loginRegisterValue : undefined, password: passValue, returnUrl: authReturnUrl, }; const res = await loginWithPassCall(apiRequest); if (!res) return; if (res.success) { const tokenRes = await generateTokenWithPassword({ username: apiRequest.email ?? (apiRequest.phoneNumber as string), password: apiRequest.password, }); auth.login({ ...tokenRes.data, }); onLoggedIn(res); toast({ message: t('verify.youHaveSuccessfullyLoggedIn'), severity: 'success', }); } else { toast({ message: res.message, severity: 'error', }); } } }; const handleLoginWithOtp = async () => { if (authType === 'phone') { await smsResendCall({ phoneNumber: countryCode + loginRegisterValue }); } else { await emailResendCall({ email: loginRegisterValue }); } onLoginWithOTP(); }; return ( {t('enterPassword.loginWithPassword')} {t('enterPassword.enterThePasswordYouSetForYourAccount')} setPassValue(e.target.value)} onBlur={handleBlur} error={!passValue && inputTouched} helperText={ !passValue && inputTouched ? t('loginForm.thisFieldIsRequired') : '' } autoFocus slotProps={{ htmlInput: { sx: { lineHeight: 1.5 } }, input: { endAdornment: ( setShowPassword(!showPassword)} > {showPassword ? ( ) : ( )} ), }, }} sx={{ my: 4 }} /> ); };