2022-10-21 18:17:04 +00:00
|
|
|
import { t } from '../../../utils/intl'
|
|
|
|
import styles from './AuthModal.module.scss'
|
|
|
|
import { clsx } from 'clsx'
|
|
|
|
import { SocialProviders } from './SocialProviders'
|
2022-10-25 16:25:42 +00:00
|
|
|
import { signIn, signSendLink } from '../../../stores/auth'
|
2022-10-21 18:17:04 +00:00
|
|
|
import { ApiError } from '../../../utils/apiClient'
|
2022-10-31 16:40:55 +00:00
|
|
|
import { createSignal, Show } from 'solid-js'
|
2022-10-21 18:17:04 +00:00
|
|
|
import { isValidEmail } from './validators'
|
|
|
|
import { email, setEmail } from './sharedLogic'
|
|
|
|
import { useRouter } from '../../../stores/router'
|
|
|
|
import type { AuthModalSearchParams } from './types'
|
2022-11-01 22:25:18 +00:00
|
|
|
import { hideModal, locale } from '../../../stores/ui'
|
2022-10-21 18:17:04 +00:00
|
|
|
|
|
|
|
type FormFields = {
|
|
|
|
email: string
|
|
|
|
password: string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ValidationErrors = Partial<Record<keyof FormFields, string>>
|
|
|
|
|
|
|
|
export const LoginForm = () => {
|
|
|
|
const [submitError, setSubmitError] = createSignal('')
|
|
|
|
const [isSubmitting, setIsSubmitting] = createSignal(false)
|
|
|
|
const [validationErrors, setValidationErrors] = createSignal<ValidationErrors>({})
|
2022-10-25 16:25:42 +00:00
|
|
|
// TODO: better solution for interactive error messages
|
|
|
|
const [isEmailNotConfirmed, setIsEmailNotConfirmed] = createSignal(false)
|
|
|
|
const [isLinkSent, setIsLinkSent] = createSignal(false)
|
2022-10-21 18:17:04 +00:00
|
|
|
|
|
|
|
const { changeSearchParam } = useRouter<AuthModalSearchParams>()
|
|
|
|
|
|
|
|
const [password, setPassword] = createSignal('')
|
|
|
|
|
|
|
|
const handleEmailInput = (newEmail: string) => {
|
|
|
|
setValidationErrors(({ email: _notNeeded, ...rest }) => rest)
|
|
|
|
setEmail(newEmail)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handlePasswordInput = (newPassword: string) => {
|
|
|
|
setValidationErrors(({ password: _notNeeded, ...rest }) => rest)
|
|
|
|
setPassword(newPassword)
|
|
|
|
}
|
|
|
|
|
2022-11-01 22:25:18 +00:00
|
|
|
const handleSendLinkAgainClick = (event: Event) => {
|
|
|
|
event.preventDefault()
|
|
|
|
setIsEmailNotConfirmed(false)
|
|
|
|
setSubmitError('')
|
|
|
|
setIsLinkSent(true)
|
|
|
|
signSendLink({ email: email(), lang: locale() })
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:17:04 +00:00
|
|
|
const handleSubmit = async (event: Event) => {
|
|
|
|
event.preventDefault()
|
|
|
|
|
2022-10-25 16:25:42 +00:00
|
|
|
setIsLinkSent(false)
|
2022-10-21 18:17:04 +00:00
|
|
|
setSubmitError('')
|
|
|
|
|
|
|
|
const newValidationErrors: ValidationErrors = {}
|
|
|
|
|
|
|
|
if (!email()) {
|
|
|
|
newValidationErrors.email = t('Please enter email')
|
|
|
|
} else if (!isValidEmail(email())) {
|
|
|
|
newValidationErrors.email = t('Invalid email')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!password()) {
|
|
|
|
newValidationErrors.password = t('Please enter password')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(newValidationErrors).length > 0) {
|
|
|
|
setValidationErrors(newValidationErrors)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
|
|
|
try {
|
|
|
|
await signIn({ email: email(), password: password() })
|
2022-10-25 16:25:42 +00:00
|
|
|
hideModal()
|
2022-10-21 18:17:04 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof ApiError) {
|
|
|
|
if (error.code === 'email_not_confirmed') {
|
|
|
|
setSubmitError(t('Please, confirm email'))
|
2022-10-25 16:25:42 +00:00
|
|
|
setIsEmailNotConfirmed(true)
|
2022-10-21 18:17:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.code === 'user_not_found') {
|
|
|
|
setSubmitError(t('Something went wrong, check email and password'))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setSubmitError(error.message)
|
|
|
|
} finally {
|
|
|
|
setIsSubmitting(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<h4>{t('Enter the Discours')}</h4>
|
|
|
|
<Show when={submitError()}>
|
|
|
|
<div class={styles.authInfo}>
|
2022-10-25 16:25:42 +00:00
|
|
|
<div class={styles.warn}>{submitError()}</div>
|
|
|
|
<Show when={isEmailNotConfirmed()}>
|
|
|
|
<a href="#" class={styles.sendLink} onClick={handleSendLinkAgainClick}>
|
|
|
|
{t('Send link again')}
|
|
|
|
</a>
|
|
|
|
</Show>
|
2022-10-21 18:17:04 +00:00
|
|
|
</div>
|
|
|
|
</Show>
|
2022-10-25 16:25:42 +00:00
|
|
|
<Show when={isLinkSent()}>
|
|
|
|
<div class={styles.authInfo}>{t('Link sent, check your email')}</div>
|
|
|
|
</Show>
|
2022-10-21 18:17:04 +00:00
|
|
|
<div class="pretty-form__item">
|
|
|
|
<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>
|
|
|
|
</div>
|
|
|
|
<Show when={validationErrors().email}>
|
|
|
|
<div class={styles.validationError}>{validationErrors().email}</div>
|
|
|
|
</Show>
|
|
|
|
<div class="pretty-form__item">
|
|
|
|
<input
|
|
|
|
id="password"
|
|
|
|
name="password"
|
|
|
|
autocomplete="password"
|
|
|
|
type="password"
|
|
|
|
placeholder={t('Password')}
|
|
|
|
onInput={(event) => handlePasswordInput(event.currentTarget.value)}
|
|
|
|
/>
|
|
|
|
<label for="password">{t('Password')}</label>
|
|
|
|
</div>
|
|
|
|
<Show when={validationErrors().password}>
|
|
|
|
<div class={styles.validationError}>{validationErrors().password}</div>
|
|
|
|
</Show>
|
|
|
|
<div>
|
|
|
|
<button class={clsx('button', styles.submitButton)} disabled={isSubmitting()} type="submit">
|
|
|
|
{isSubmitting() ? '...' : t('Enter')}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div class={styles.authActions}>
|
|
|
|
<a
|
|
|
|
href="#"
|
|
|
|
onClick={(ev) => {
|
|
|
|
ev.preventDefault()
|
|
|
|
changeSearchParam('mode', 'forgot-password')
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t('Forgot password?')}
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<SocialProviders />
|
|
|
|
|
|
|
|
<div class={styles.authControl}>
|
|
|
|
<span class={styles.authLink} onClick={() => changeSearchParam('mode', 'register')}>
|
|
|
|
{t('I have no account yet')}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
)
|
|
|
|
}
|