2023-03-19 19:03:51 +01:00
|
|
|
// Hooks
|
|
|
|
import { useEffect, useState } from 'react'
|
2023-04-16 19:34:31 +02:00
|
|
|
import { useAccount } from 'shared/hooks/use-account.mjs'
|
2023-03-24 17:43:38 +01:00
|
|
|
import { useBackend } from 'shared/hooks/use-backend.mjs'
|
2023-03-19 19:03:51 +01:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
// Dependencies
|
|
|
|
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
|
|
|
|
import Link from 'next/link'
|
|
|
|
// Components
|
2023-03-27 19:07:48 +02:00
|
|
|
import { PageWrapper } from 'shared/components/wrappers/page.mjs'
|
2023-03-19 19:03:51 +01:00
|
|
|
import { BareLayout } from 'site/components/layouts/bare.mjs'
|
|
|
|
import { Spinner } from 'shared/components/spinner.mjs'
|
|
|
|
import { Robot } from 'shared/components/robot/index.mjs'
|
|
|
|
import { FlexButtonText } from 'site/components/buttons/flex-button-text.mjs'
|
|
|
|
import { LeftIcon, KeyIcon } from 'shared/components/icons.mjs'
|
|
|
|
|
|
|
|
// Translation namespaces used on this page
|
|
|
|
const ns = Array.from(new Set(['signin', 'locales', 'themes']))
|
|
|
|
|
|
|
|
export const SigninLinkExpired = () => {
|
|
|
|
const { t } = useTranslation('signin')
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="p-8 max-w-md">
|
|
|
|
<h1 className="text-center">{t('signInFailed')}</h1>
|
|
|
|
<Robot pose="shrug" className="w-2/3 m-auto" embed />
|
|
|
|
<Link className="btn btn-primary btn-lg w-full" href="/signin">
|
|
|
|
<FlexButtonText>
|
|
|
|
<LeftIcon />
|
|
|
|
{t('back')}
|
|
|
|
<KeyIcon />
|
|
|
|
</FlexButtonText>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
const Wrapper = ({ page, t, children }) => (
|
|
|
|
<PageWrapper {...page} title={t('signin:oneMomentPlease')} layout={BareLayout} footer={false}>
|
2023-03-19 19:03:51 +01:00
|
|
|
<section className="m-0 p-0 w-full">
|
|
|
|
<div className="mt-4 lg:mt-32 max-w-xl m-auto">{children}</div>
|
|
|
|
</section>
|
|
|
|
</PageWrapper>
|
|
|
|
)
|
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
/*
|
|
|
|
* Each page MUST be wrapped in the PageWrapper component.
|
|
|
|
* You also MUST spread props.page into this wrapper component
|
|
|
|
* and set path and locale when it's dynamic (like on this page)
|
|
|
|
*/
|
|
|
|
const ConfirmSignInPage = ({ page }) => {
|
2023-04-16 19:34:31 +02:00
|
|
|
const router = useRouter()
|
|
|
|
// Get confirmation ID and check from url
|
|
|
|
const [confirmationId, confirmationCheck] = router.asPath.slice(1).split('/').slice(2)
|
2023-04-16 17:13:18 +02:00
|
|
|
const { setAccount, setToken } = useAccount()
|
2023-03-24 17:43:38 +01:00
|
|
|
const backend = useBackend()
|
2023-03-19 19:03:51 +01:00
|
|
|
const { t } = useTranslation(ns)
|
|
|
|
|
|
|
|
const [error, setError] = useState(false)
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-05-17 14:04:57 +02:00
|
|
|
const storeAccount = async (data) => {
|
|
|
|
if (data?.token && data?.account) {
|
|
|
|
setToken(data.token)
|
|
|
|
setAccount(data.account)
|
|
|
|
router.push('/account')
|
|
|
|
} else {
|
|
|
|
setError(data)
|
|
|
|
}
|
|
|
|
}
|
2023-03-19 19:03:51 +01:00
|
|
|
// Async inside useEffect requires this approach
|
|
|
|
const getConfirmation = async () => {
|
|
|
|
// Reach out to backend
|
2023-04-16 19:34:31 +02:00
|
|
|
const result = await backend.signInFromLink({
|
2023-03-19 19:03:51 +01:00
|
|
|
id: confirmationId,
|
|
|
|
check: confirmationCheck,
|
|
|
|
})
|
2023-04-16 19:34:31 +02:00
|
|
|
if (result?.data?.token) return storeAccount(result.data)
|
2023-03-19 19:03:51 +01:00
|
|
|
if (result.status === 404) return setError(404)
|
|
|
|
|
|
|
|
return setError(true)
|
|
|
|
}
|
|
|
|
// Call async method
|
|
|
|
getConfirmation()
|
2023-05-17 14:04:57 +02:00
|
|
|
}, [backend, confirmationCheck, confirmationId, router, setAccount, setToken])
|
2023-03-19 19:03:51 +01:00
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
if (page) page.path = ['confirm', 'emailchange', confirmationId]
|
|
|
|
|
2023-03-19 19:03:51 +01:00
|
|
|
// Short-circuit errors
|
|
|
|
if (error)
|
|
|
|
return (
|
2023-04-28 21:23:06 +02:00
|
|
|
<Wrapper page={page} t={t}>
|
2023-03-19 19:03:51 +01:00
|
|
|
<SigninLinkExpired />
|
|
|
|
</Wrapper>
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
2023-04-28 21:23:06 +02:00
|
|
|
<Wrapper page={page} t={t}>
|
2023-03-19 19:03:51 +01:00
|
|
|
<h1>{t('oneMomentPlease')}</h1>
|
|
|
|
<Spinner className="w-8 h-8 m-auto animate-spin" />
|
|
|
|
</Wrapper>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ConfirmSignInPage
|
|
|
|
|
|
|
|
export async function getStaticProps({ locale }) {
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
...(await serverSideTranslations(locale, ns)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getStaticPaths() {
|
|
|
|
return {
|
|
|
|
paths: [],
|
|
|
|
fallback: true,
|
|
|
|
}
|
|
|
|
}
|