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