auth-error-fix
This commit is contained in:
parent
aca1358c18
commit
fce3d9cf21
|
@ -12,44 +12,26 @@ import styles from './AuthModal.module.scss'
|
|||
|
||||
export const EmailConfirm = () => {
|
||||
const { t } = useLocalize()
|
||||
const { searchParams } = useRouter<ConfirmEmailSearchParams>()
|
||||
const {
|
||||
actions: { confirmEmail },
|
||||
session,
|
||||
} = useSession()
|
||||
const [isTokenExpired, setIsTokenExpired] = createSignal(false) // TODO: handle expired token in context/session
|
||||
const [isTokenInvalid, setIsTokenInvalid] = createSignal(false) // TODO: handle invalid token in context/session
|
||||
|
||||
createEffect(async () => {
|
||||
const token = searchParams()?.access_token
|
||||
if (token) await confirmEmail({ token })
|
||||
const { session, authError } = useSession()
|
||||
const [email, setEmail] = createSignal('')
|
||||
const [emailConfirmed, setEmailConfirmed] = createSignal(false)
|
||||
createEffect(() => {
|
||||
setEmail(session()?.user?.email)
|
||||
setEmailConfirmed(session()?.user?.email_verified)
|
||||
})
|
||||
|
||||
const email = createMemo(() => session()?.user?.email)
|
||||
const confirmedEmail = createMemo(() => session()?.user?.email_verified)
|
||||
|
||||
return (
|
||||
<div>
|
||||
{/* TODO: texts */}
|
||||
<Show when={isTokenExpired()}>
|
||||
<div class={styles.title}>Ссылка больше не действительна</div>
|
||||
<Show when={authError()}>
|
||||
<div class={styles.title}>{authError()}</div>
|
||||
<div class={styles.text}>
|
||||
<a href="/?modal=auth&mode=login">
|
||||
{/*TODO: temp solution, should be send link again */}
|
||||
Вход
|
||||
{t('Enter')}
|
||||
</a>
|
||||
</div>
|
||||
</Show>
|
||||
<Show when={isTokenInvalid()}>
|
||||
<div class={styles.title}>Неправильная ссылка</div>
|
||||
<div class={styles.text}>
|
||||
<a href="/?modal=auth&mode=login">
|
||||
{/*TODO: temp solution, should be send link again */}
|
||||
Вход
|
||||
</a>
|
||||
</div>
|
||||
</Show>
|
||||
<Show when={confirmedEmail()}>
|
||||
<Show when={emailConfirmed()}>
|
||||
<div class={styles.title}>{t('Hooray! Welcome!')}</div>
|
||||
<div class={styles.text}>
|
||||
{t("You've confirmed email")} {email()}
|
||||
|
|
|
@ -40,6 +40,7 @@ export type SessionContextType = {
|
|||
config: ConfigType
|
||||
session: Resource<AuthToken>
|
||||
author: Resource<Author | null>
|
||||
authError: Accessor<string>
|
||||
isSessionLoaded: Accessor<boolean>
|
||||
subscriptions: Accessor<Result>
|
||||
isAuthWithCallback: Accessor<() => void>
|
||||
|
@ -104,17 +105,18 @@ export const SessionProvider = (props: {
|
|||
})
|
||||
|
||||
// load
|
||||
|
||||
const [configuration, setConfig] = createSignal<ConfigType>(defaultConfig)
|
||||
const authorizer = createMemo(() => new Authorizer(defaultConfig))
|
||||
const [isSessionLoaded, setIsSessionLoaded] = createSignal(false)
|
||||
const [authError, setAuthError] = createSignal('')
|
||||
const [session, { refetch: loadSession, mutate: setSession }] = createResource<AuthToken>(
|
||||
async () => {
|
||||
try {
|
||||
console.info('[context.session] loading session')
|
||||
return await authorizer().getSession()
|
||||
} catch {
|
||||
console.info('[context.session] cannot refresh session')
|
||||
} catch (e) {
|
||||
console.info('[context.session] cannot refresh session', e)
|
||||
setAuthError(e)
|
||||
return null
|
||||
}
|
||||
},
|
||||
|
@ -141,7 +143,7 @@ export const SessionProvider = (props: {
|
|||
setSubscriptions(result || EMPTY_SUBSCRIPTIONS)
|
||||
}
|
||||
|
||||
// session postload effect
|
||||
// when session is loaded
|
||||
createEffect(async () => {
|
||||
if (session()) {
|
||||
const token = session()?.access_token
|
||||
|
@ -158,22 +160,16 @@ export const SessionProvider = (props: {
|
|||
if (a) {
|
||||
console.log('[context.session] author profile and subs loaded', author())
|
||||
} else {
|
||||
setSubscriptions(EMPTY_SUBSCRIPTIONS)
|
||||
setAuthor(null)
|
||||
console.warn('[context.session] author is not loaded')
|
||||
}
|
||||
setIsSessionLoaded(true)
|
||||
}
|
||||
setIsSessionLoaded(true)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
createEffect(() => {
|
||||
if (session() !== null && author() === null) {
|
||||
setIsSessionLoaded(true)
|
||||
setAuthor(null)
|
||||
setSubscriptions(EMPTY_SUBSCRIPTIONS)
|
||||
}
|
||||
})
|
||||
|
||||
// initial effect
|
||||
onMount(async () => {
|
||||
const metaRes = await authorizer().getMetaData()
|
||||
|
@ -242,9 +238,13 @@ export const SessionProvider = (props: {
|
|||
|
||||
const confirmEmail = async (input: VerifyEmailInput) => {
|
||||
console.debug(`[context.session] calling authorizer's verify email with`, input)
|
||||
const at: void | AuthToken = await authorizer().verifyEmail(input)
|
||||
if (at) setSession(at)
|
||||
return at
|
||||
try {
|
||||
const at: void | AuthToken = await authorizer().verifyEmail(input)
|
||||
if (at) setSession(at)
|
||||
return at
|
||||
} catch (e) {
|
||||
console.debug(e)
|
||||
}
|
||||
}
|
||||
|
||||
const isAuthenticated = createMemo(() => Boolean(author()))
|
||||
|
@ -264,6 +264,7 @@ export const SessionProvider = (props: {
|
|||
changePassword,
|
||||
}
|
||||
const value: SessionContextType = {
|
||||
authError,
|
||||
config: configuration(),
|
||||
session,
|
||||
subscriptions,
|
||||
|
|
|
@ -95,7 +95,7 @@ export const apiClient = {
|
|||
},
|
||||
getAllAuthors: async () => {
|
||||
const response = await publicGraphQLClient.query(authorsAll, {}).toPromise()
|
||||
if (!response.data) console.error('[graphql.client.core] load_authors_all', response)
|
||||
if (!response.data) console.error('[graphql.client.core] getAllAuthors', response)
|
||||
|
||||
return response.data.get_authors_all
|
||||
},
|
||||
|
|
|
@ -2,7 +2,7 @@ import { gql } from '@urql/core'
|
|||
|
||||
export default gql`
|
||||
query {
|
||||
get_authors_all() {
|
||||
get_authors_all {
|
||||
id
|
||||
slug
|
||||
name
|
||||
|
|
Loading…
Reference in New Issue
Block a user