refactoring
This commit is contained in:
parent
ea24995c0e
commit
fdceeb6060
|
@ -1,7 +1,7 @@
|
|||
import type { AuthModalSearchParams } from './types'
|
||||
|
||||
import { clsx } from 'clsx'
|
||||
import { createSignal, Show } from 'solid-js'
|
||||
import { createEffect, createSignal, Show } from 'solid-js'
|
||||
|
||||
import { useLocalize } from '../../../context/localize'
|
||||
import { useSession } from '../../../context/session'
|
||||
|
@ -106,26 +106,22 @@ export const LoginForm = () => {
|
|||
setIsSubmitting(true)
|
||||
|
||||
try {
|
||||
await signIn({ email: email(), password: password() })
|
||||
|
||||
const { errors } = await signIn({ email: email(), password: password() })
|
||||
if (errors?.length > 0) {
|
||||
if (errors.some((error) => error.message.includes('bad user credentials'))) {
|
||||
setValidationErrors((prev) => ({
|
||||
...prev,
|
||||
password: t('Something went wrong, check email and password'),
|
||||
}))
|
||||
} else {
|
||||
setSubmitError(t('Error'))
|
||||
}
|
||||
return
|
||||
}
|
||||
hideModal()
|
||||
|
||||
showSnackbar({ body: t('Welcome!') })
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
if (error instanceof ApiError) {
|
||||
if (error.code === 'email_not_confirmed') {
|
||||
setSubmitError(t('Please, confirm email'))
|
||||
setIsEmailNotConfirmed(true)
|
||||
|
||||
return
|
||||
}
|
||||
if (error.code === 'user_not_found') {
|
||||
setSubmitError(t('Something went wrong, check email and password'))
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
setSubmitError(error.message)
|
||||
} finally {
|
||||
setIsSubmitting(false)
|
||||
|
@ -170,6 +166,11 @@ export const LoginForm = () => {
|
|||
</div>
|
||||
|
||||
<PasswordField variant={'login'} onInput={(value) => handlePasswordInput(value)} />
|
||||
<Show when={validationErrors().password}>
|
||||
<div class={styles.validationError} style={{ position: 'static', 'font-size': '1.4rem' }}>
|
||||
{validationErrors().password}
|
||||
</div>
|
||||
</Show>
|
||||
|
||||
<div>
|
||||
<button class={clsx('button', styles.submitButton)} disabled={isSubmitting()} type="submit">
|
||||
|
|
|
@ -6,7 +6,6 @@ import { Show, createSignal } from 'solid-js'
|
|||
|
||||
import { useLocalize } from '../../../context/localize'
|
||||
import { useSession } from '../../../context/session'
|
||||
// import { ApiError } from '../../../graphql/error'
|
||||
import { checkEmail, useEmailChecks } from '../../../stores/emailChecks'
|
||||
import { useRouter } from '../../../stores/router'
|
||||
import { hideModal } from '../../../stores/ui'
|
||||
|
@ -113,34 +112,30 @@ export const RegisterForm = () => {
|
|||
confirm_password: password(),
|
||||
redirect_uri: window.location.origin,
|
||||
}
|
||||
await signUp(opts)
|
||||
|
||||
const { errors } = await signUp(opts)
|
||||
if (errors && errors.some((error) => error.message.includes('has already signed up'))) {
|
||||
setValidationErrors((prev) => ({
|
||||
...prev,
|
||||
email: (
|
||||
<>
|
||||
{t('User with this email already exists')},{' '}
|
||||
<span
|
||||
class={'link'}
|
||||
onClick={() =>
|
||||
changeSearchParams({
|
||||
mode: 'login',
|
||||
})
|
||||
}
|
||||
>
|
||||
{t('sign in')}
|
||||
</span>
|
||||
</>
|
||||
),
|
||||
}))
|
||||
}
|
||||
setIsSuccess(true)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
if (error) {
|
||||
if (error.message.includes('has already signed up')) {
|
||||
setValidationErrors((errors) => ({
|
||||
...errors,
|
||||
email: (
|
||||
<>
|
||||
{t('User with this email already exists')},{' '}
|
||||
<span
|
||||
class={'link'}
|
||||
onClick={() =>
|
||||
changeSearchParams({
|
||||
mode: 'login',
|
||||
})
|
||||
}
|
||||
>
|
||||
{t('sign in')}
|
||||
</span>
|
||||
</>
|
||||
),
|
||||
}))
|
||||
}
|
||||
console.error(error)
|
||||
}
|
||||
} finally {
|
||||
setIsSubmitting(false)
|
||||
}
|
||||
|
|
|
@ -59,8 +59,8 @@ export type SessionContextType = {
|
|||
callback: (() => Promise<void>) | (() => void),
|
||||
modalSource: AuthModalSource,
|
||||
) => void
|
||||
signUp: (params: SignupInput) => Promise<AuthToken | void>
|
||||
signIn: (params: LoginInput) => Promise<void>
|
||||
signUp: (params: SignupInput) => Promise<{ data: AuthToken; errors: Error[] }>
|
||||
signIn: (params: LoginInput) => Promise<{ data: AuthToken; errors: Error[] }>
|
||||
signOut: () => Promise<void>
|
||||
oauth: (provider: string) => Promise<void>
|
||||
forgotPassword: (
|
||||
|
@ -273,16 +273,20 @@ export const SessionProvider = (props: {
|
|||
})
|
||||
|
||||
// authorizer api proxy methods
|
||||
const authenticate = async (authFunction, params) => {
|
||||
const resp = await authFunction(params)
|
||||
console.debug('[context.session] authenticate:', resp)
|
||||
if (resp?.data && !resp.errors) {
|
||||
setSession(resp.data)
|
||||
}
|
||||
return { data: resp?.data, errors: resp?.errors }
|
||||
}
|
||||
const signUp = async (params: SignupInput) => {
|
||||
const authResult: ApiResponse<AuthToken> = await authorizer().signup(params)
|
||||
if (authResult?.data) setSession(authResult.data)
|
||||
if (authResult?.errors) console.error(authResult.errors)
|
||||
return authenticate(authorizer().signup, params)
|
||||
}
|
||||
|
||||
const signIn = async (params: LoginInput) => {
|
||||
const authResult: ApiResponse<AuthToken> = await authorizer().login(params)
|
||||
if (authResult?.data) setSession(authResult.data)
|
||||
if (authResult?.errors) console.error(authResult.errors)
|
||||
return authenticate(authorizer().login, params)
|
||||
}
|
||||
|
||||
const signOut = async () => {
|
||||
|
|
|
@ -28,7 +28,6 @@ export const inboxClient = {
|
|||
|
||||
loadChats: async (options: QueryLoad_ChatsArgs): Promise<Chat[]> => {
|
||||
const resp = await inboxClient.private.query(myChats, options).toPromise()
|
||||
console.log('!!! resp:', resp)
|
||||
return resp.data.load_chats.chats
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user