2023-11-14 15:10:00 +00:00
|
|
|
import type { AuthModalSearchParams } from './types'
|
|
|
|
|
2022-10-21 18:17:04 +00:00
|
|
|
import { clsx } from 'clsx'
|
2024-05-01 14:41:59 +00:00
|
|
|
import { JSX, Show, createSignal } from 'solid-js'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
|
|
|
import { useLocalize } from '../../../context/localize'
|
2022-11-14 10:02:08 +00:00
|
|
|
import { useSession } from '../../../context/session'
|
2023-11-14 15:10:00 +00:00
|
|
|
import { useSnackbar } from '../../../context/snackbar'
|
|
|
|
import { useRouter } from '../../../stores/router'
|
|
|
|
import { hideModal } from '../../../stores/ui'
|
2023-07-02 05:08:42 +00:00
|
|
|
import { validateEmail } from '../../../utils/validateEmail'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
2023-07-30 12:31:54 +00:00
|
|
|
import { AuthModalHeader } from './AuthModalHeader'
|
2023-12-21 10:02:28 +00:00
|
|
|
import { PasswordField } from './PasswordField'
|
2023-11-14 15:10:00 +00:00
|
|
|
import { SocialProviders } from './SocialProviders'
|
2024-02-04 11:25:21 +00:00
|
|
|
import { email, setEmail } from './sharedLogic'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
|
|
|
import styles from './AuthModal.module.scss'
|
2022-10-21 18:17:04 +00:00
|
|
|
|
|
|
|
type FormFields = {
|
|
|
|
email: string
|
|
|
|
password: string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ValidationErrors = Partial<Record<keyof FormFields, string>>
|
|
|
|
|
|
|
|
export const LoginForm = () => {
|
2024-02-04 17:40:15 +00:00
|
|
|
const { changeSearchParams } = useRouter<AuthModalSearchParams>()
|
2023-11-28 18:04:51 +00:00
|
|
|
const { t } = useLocalize()
|
2024-03-07 08:07:46 +00:00
|
|
|
const [submitError, setSubmitError] = createSignal<string | JSX.Element>()
|
2022-10-21 18:17:04 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = createSignal(false)
|
2024-02-04 17:40:15 +00:00
|
|
|
const [password, setPassword] = createSignal('')
|
2022-10-21 18:17:04 +00:00
|
|
|
const [validationErrors, setValidationErrors] = createSignal<ValidationErrors>({})
|
2024-05-01 14:41:59 +00:00
|
|
|
// FIXME: use signal or remove
|
|
|
|
const [_isLinkSent, setIsLinkSent] = createSignal(false)
|
2023-08-13 13:36:18 +00:00
|
|
|
const authFormRef: { current: HTMLFormElement } = { current: null }
|
2024-02-04 17:40:15 +00:00
|
|
|
const { showSnackbar } = useSnackbar()
|
|
|
|
const { signIn } = useSession()
|
2022-10-21 18:17:04 +00:00
|
|
|
|
|
|
|
const handleEmailInput = (newEmail: string) => {
|
|
|
|
setValidationErrors(({ email: _notNeeded, ...rest }) => rest)
|
2024-01-13 14:22:04 +00:00
|
|
|
setEmail(newEmail.toLowerCase())
|
2022-10-21 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const handlePasswordInput = (newPassword: string) => {
|
|
|
|
setValidationErrors(({ password: _notNeeded, ...rest }) => rest)
|
|
|
|
setPassword(newPassword)
|
|
|
|
}
|
|
|
|
|
2024-02-05 15:04:23 +00:00
|
|
|
const handleSendLinkAgainClick = (event: Event) => {
|
2022-11-01 22:25:18 +00:00
|
|
|
event.preventDefault()
|
2023-08-13 13:36:18 +00:00
|
|
|
|
|
|
|
setIsLinkSent(true)
|
2024-03-07 08:07:46 +00:00
|
|
|
setSubmitError()
|
|
|
|
changeSearchParams({ mode: 'send-confirm-email' })
|
2022-11-01 22:25:18 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 08:07:46 +00:00
|
|
|
const preSendValidate = async (value: string, type: 'email' | 'password'): Promise<boolean> => {
|
|
|
|
if (type === 'email') {
|
|
|
|
if (value === '' || !validateEmail(value)) {
|
|
|
|
setValidationErrors((prev) => ({
|
|
|
|
...prev,
|
|
|
|
email: t('Invalid email'),
|
|
|
|
}))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if (type === 'password') {
|
|
|
|
if (value === '') {
|
|
|
|
setValidationErrors((prev) => ({
|
|
|
|
...prev,
|
|
|
|
password: t('Please enter password'),
|
|
|
|
}))
|
|
|
|
return false
|
2024-02-04 17:40:15 +00:00
|
|
|
}
|
2022-10-21 18:17:04 +00:00
|
|
|
}
|
2024-03-07 08:07:46 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
const handleSubmit = async (event: Event) => {
|
|
|
|
event.preventDefault()
|
2022-10-21 18:17:04 +00:00
|
|
|
|
2024-03-07 08:07:46 +00:00
|
|
|
await preSendValidate(email(), 'email')
|
|
|
|
await preSendValidate(password(), 'password')
|
2022-10-21 18:17:04 +00:00
|
|
|
|
2024-03-07 08:07:46 +00:00
|
|
|
setIsLinkSent(false)
|
|
|
|
setSubmitError()
|
2023-08-13 13:36:18 +00:00
|
|
|
|
2024-03-07 08:07:46 +00:00
|
|
|
if (Object.keys(validationErrors()).length > 0) {
|
2023-08-13 13:36:18 +00:00
|
|
|
authFormRef.current
|
2024-03-07 08:07:46 +00:00
|
|
|
.querySelector<HTMLInputElement>(`input[name="${Object.keys(validationErrors())[0]}"]`)
|
2024-02-06 14:34:27 +00:00
|
|
|
?.focus()
|
2022-10-21 18:17:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
|
|
|
try {
|
2024-02-01 20:34:53 +00:00
|
|
|
const { errors } = await signIn({ email: email(), password: password() })
|
2024-03-07 08:07:46 +00:00
|
|
|
console.error('[signIn errors]', errors)
|
2024-02-01 20:34:53 +00:00
|
|
|
if (errors?.length > 0) {
|
2024-05-06 14:42:18 +00:00
|
|
|
if (
|
|
|
|
errors.some(
|
|
|
|
(error) =>
|
|
|
|
error.message.includes('bad user credentials') || error.message.includes('user not found'),
|
|
|
|
)
|
|
|
|
) {
|
2024-02-01 20:34:53 +00:00
|
|
|
setValidationErrors((prev) => ({
|
|
|
|
...prev,
|
|
|
|
password: t('Something went wrong, check email and password'),
|
|
|
|
}))
|
2024-03-07 08:07:46 +00:00
|
|
|
} else if (errors.some((error) => error.message.includes('user not found'))) {
|
|
|
|
setSubmitError('Пользователь не найден')
|
|
|
|
} else if (errors.some((error) => error.message.includes('email not verified'))) {
|
|
|
|
setSubmitError(
|
|
|
|
<div class={styles.info}>
|
|
|
|
{t('This email is not verified')}
|
|
|
|
{'. '}
|
|
|
|
<span class={'link'} onClick={handleSendLinkAgainClick}>
|
|
|
|
{t('Send link again')}
|
|
|
|
</span>
|
|
|
|
</div>,
|
|
|
|
)
|
2024-02-01 20:34:53 +00:00
|
|
|
} else {
|
2024-03-07 08:07:46 +00:00
|
|
|
setSubmitError(t('Error', errors[0].message))
|
2024-02-01 20:34:53 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-10-25 16:25:42 +00:00
|
|
|
hideModal()
|
2023-02-10 11:11:24 +00:00
|
|
|
showSnackbar({ body: t('Welcome!') })
|
2022-10-21 18:17:04 +00:00
|
|
|
} catch (error) {
|
2023-12-24 08:16:41 +00:00
|
|
|
console.error(error)
|
2022-10-21 18:17:04 +00:00
|
|
|
setSubmitError(error.message)
|
|
|
|
} finally {
|
|
|
|
setIsSubmitting(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-08-13 13:36:18 +00:00
|
|
|
<form onSubmit={handleSubmit} class={styles.authForm} ref={(el) => (authFormRef.current = el)}>
|
2023-05-18 20:02:19 +00:00
|
|
|
<div>
|
2023-07-30 12:31:54 +00:00
|
|
|
<AuthModalHeader modalType="login" />
|
2023-08-13 13:36:18 +00:00
|
|
|
<div
|
|
|
|
class={clsx('pretty-form__item', {
|
2023-11-14 15:10:00 +00:00
|
|
|
'pretty-form__item--error': validationErrors().email,
|
2023-08-13 13:36:18 +00:00
|
|
|
})}
|
|
|
|
>
|
2023-05-18 20:02:19 +00:00
|
|
|
<input
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
autocomplete="email"
|
|
|
|
type="email"
|
|
|
|
value={email()}
|
|
|
|
placeholder={t('Email')}
|
|
|
|
onInput={(event) => handleEmailInput(event.currentTarget.value)}
|
|
|
|
/>
|
|
|
|
<label for="email">{t('Email')}</label>
|
2023-08-30 21:30:15 +00:00
|
|
|
<Show when={validationErrors().email}>
|
|
|
|
<div class={styles.validationError}>{validationErrors().email}</div>
|
|
|
|
</Show>
|
2023-05-18 20:02:19 +00:00
|
|
|
</div>
|
|
|
|
|
2024-03-07 08:07:46 +00:00
|
|
|
<PasswordField
|
|
|
|
variant={'login'}
|
|
|
|
setError={validationErrors().password}
|
2024-03-11 07:41:31 +00:00
|
|
|
onBlur={(value) => handlePasswordInput(value)}
|
2024-03-07 08:07:46 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<Show when={submitError()}>
|
2024-03-11 13:23:06 +00:00
|
|
|
<div class={clsx('form-message--error', styles.validationError)}>{submitError()}</div>
|
2024-02-01 20:34:53 +00:00
|
|
|
</Show>
|
2023-05-11 17:20:40 +00:00
|
|
|
|
2023-05-18 20:02:19 +00:00
|
|
|
<div>
|
|
|
|
<button class={clsx('button', styles.submitButton)} disabled={isSubmitting()} type="submit">
|
|
|
|
{isSubmitting() ? '...' : t('Enter')}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div class={styles.authActions}>
|
2023-09-09 12:05:26 +00:00
|
|
|
<span
|
2023-09-29 12:48:58 +00:00
|
|
|
class="link"
|
|
|
|
onClick={() =>
|
2023-12-20 08:07:57 +00:00
|
|
|
changeSearchParams({
|
2024-02-06 15:02:57 +00:00
|
|
|
mode: 'send-reset-link',
|
2023-09-29 12:48:58 +00:00
|
|
|
})
|
|
|
|
}
|
2023-05-18 20:02:19 +00:00
|
|
|
>
|
2024-03-07 08:07:46 +00:00
|
|
|
{t('Forgot password?')}
|
2023-09-09 12:05:26 +00:00
|
|
|
</span>
|
2023-05-18 20:02:19 +00:00
|
|
|
</div>
|
2022-10-21 18:17:04 +00:00
|
|
|
</div>
|
|
|
|
|
2023-05-18 20:02:19 +00:00
|
|
|
<div>
|
|
|
|
<SocialProviders />
|
2022-10-21 18:17:04 +00:00
|
|
|
|
2023-05-18 20:02:19 +00:00
|
|
|
<div class={styles.authControl}>
|
2023-09-29 12:48:58 +00:00
|
|
|
<span
|
|
|
|
class={styles.authLink}
|
|
|
|
onClick={() =>
|
2023-12-20 08:07:57 +00:00
|
|
|
changeSearchParams({
|
2023-11-14 15:10:00 +00:00
|
|
|
mode: 'register',
|
2023-09-29 12:48:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
2023-05-18 20:02:19 +00:00
|
|
|
{t('I have no account yet')}
|
|
|
|
</span>
|
|
|
|
</div>
|
2022-10-21 18:17:04 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
)
|
|
|
|
}
|