mirror of
https://github.com/langgenius/dify.git
synced 2026-04-05 10:25:48 +08:00
Signed-off-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Signed-off-by: lyzno1 <yuanyouhuilyz@gmail.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Yunlu Wen <wylswz@163.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: GareArc <chen4851@purdue.edu> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: Davide Delbianco <davide.delbianco@outlook.com> Co-authored-by: minglu7 <1347866672@qq.com> Co-authored-by: Ponder <ruan.lj@foxmail.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: heyszt <270985384@qq.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: Eric Guo <eric.guocz@gmail.com> Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: XlKsyt <caixuesen@outlook.com> Co-authored-by: Dhruv Gorasiya <80987415+DhruvGorasiya@users.noreply.github.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: GuanMu <ballmanjq@gmail.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Tonlo <123lzs123@gmail.com> Co-authored-by: Yusuke Yamada <yamachu.dev@gmail.com> Co-authored-by: Novice <novice12185727@gmail.com> Co-authored-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: Ademílson Tonato <ademilsonft@outlook.com> Co-authored-by: znn <jubinkumarsoni@gmail.com> Co-authored-by: yangzheli <43645580+yangzheli@users.noreply.github.com>
122 lines
4.7 KiB
TypeScript
122 lines
4.7 KiB
TypeScript
'use client'
|
|
import { RiArrowLeftLine, RiMailSendFill } from '@remixicon/react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { useCallback, useState } from 'react'
|
|
import { useRouter, useSearchParams } from 'next/navigation'
|
|
import { useContext } from 'use-context-selector'
|
|
import Countdown from '@/app/components/signin/countdown'
|
|
import Button from '@/app/components/base/button'
|
|
import Input from '@/app/components/base/input'
|
|
import Toast from '@/app/components/base/toast'
|
|
import { sendWebAppEMailLoginCode, webAppEmailLoginWithCode } from '@/service/common'
|
|
import I18NContext from '@/context/i18n'
|
|
import { setWebAppAccessToken, setWebAppPassport } from '@/service/webapp-auth'
|
|
import { fetchAccessToken } from '@/service/share'
|
|
|
|
export default function CheckCode() {
|
|
const { t } = useTranslation()
|
|
const router = useRouter()
|
|
const searchParams = useSearchParams()
|
|
const email = decodeURIComponent(searchParams.get('email') as string)
|
|
const token = decodeURIComponent(searchParams.get('token') as string)
|
|
const [code, setVerifyCode] = useState('')
|
|
const [loading, setIsLoading] = useState(false)
|
|
const { locale } = useContext(I18NContext)
|
|
const redirectUrl = searchParams.get('redirect_url')
|
|
|
|
const getAppCodeFromRedirectUrl = useCallback(() => {
|
|
if (!redirectUrl)
|
|
return null
|
|
const url = new URL(`${window.location.origin}${decodeURIComponent(redirectUrl)}`)
|
|
const appCode = url.pathname.split('/').pop()
|
|
if (!appCode)
|
|
return null
|
|
|
|
return appCode
|
|
}, [redirectUrl])
|
|
|
|
const verify = async () => {
|
|
try {
|
|
const appCode = getAppCodeFromRedirectUrl()
|
|
if (!code.trim()) {
|
|
Toast.notify({
|
|
type: 'error',
|
|
message: t('login.checkCode.emptyCode'),
|
|
})
|
|
return
|
|
}
|
|
if (!/\d{6}/.test(code)) {
|
|
Toast.notify({
|
|
type: 'error',
|
|
message: t('login.checkCode.invalidCode'),
|
|
})
|
|
return
|
|
}
|
|
if (!redirectUrl || !appCode) {
|
|
Toast.notify({
|
|
type: 'error',
|
|
message: t('login.error.redirectUrlMissing'),
|
|
})
|
|
return
|
|
}
|
|
setIsLoading(true)
|
|
const ret = await webAppEmailLoginWithCode({ email, code, token })
|
|
if (ret.result === 'success') {
|
|
setWebAppAccessToken(ret.data.access_token)
|
|
const { access_token } = await fetchAccessToken({ appCode: appCode! })
|
|
setWebAppPassport(appCode!, access_token)
|
|
router.replace(decodeURIComponent(redirectUrl))
|
|
}
|
|
}
|
|
catch (error) { console.error(error) }
|
|
finally {
|
|
setIsLoading(false)
|
|
}
|
|
}
|
|
|
|
const resendCode = async () => {
|
|
try {
|
|
const ret = await sendWebAppEMailLoginCode(email, locale)
|
|
if (ret.result === 'success') {
|
|
const params = new URLSearchParams(searchParams)
|
|
params.set('token', encodeURIComponent(ret.data))
|
|
router.replace(`/webapp-signin/check-code?${params.toString()}`)
|
|
}
|
|
}
|
|
catch (error) { console.error(error) }
|
|
}
|
|
|
|
return <div className='flex w-[400px] flex-col gap-3'>
|
|
<div className='inline-flex h-14 w-14 items-center justify-center rounded-2xl border border-components-panel-border-subtle bg-background-default-dodge shadow-lg'>
|
|
<RiMailSendFill className='h-6 w-6 text-2xl text-text-accent-light-mode-only' />
|
|
</div>
|
|
<div className='pb-4 pt-2'>
|
|
<h2 className='title-4xl-semi-bold text-text-primary'>{t('login.checkCode.checkYourEmail')}</h2>
|
|
<p className='body-md-regular mt-2 text-text-secondary'>
|
|
<span>
|
|
{t('login.checkCode.tipsPrefix')}
|
|
<strong>{email}</strong>
|
|
</span>
|
|
<br />
|
|
{t('login.checkCode.validTime')}
|
|
</p>
|
|
</div>
|
|
|
|
<form action="">
|
|
<label htmlFor="code" className='system-md-semibold mb-1 text-text-secondary'>{t('login.checkCode.verificationCode')}</label>
|
|
<Input value={code} onChange={e => setVerifyCode(e.target.value)} maxLength={6} className='mt-1' placeholder={t('login.checkCode.verificationCodePlaceholder') || ''} />
|
|
<Button loading={loading} disabled={loading} className='my-3 w-full' variant='primary' onClick={verify}>{t('login.checkCode.verify')}</Button>
|
|
<Countdown onResend={resendCode} />
|
|
</form>
|
|
<div className='py-2'>
|
|
<div className='h-px bg-gradient-to-r from-background-gradient-mask-transparent via-divider-regular to-background-gradient-mask-transparent'></div>
|
|
</div>
|
|
<div onClick={() => router.back()} className='flex h-9 cursor-pointer items-center justify-center text-text-tertiary'>
|
|
<div className='bg-background-default-dimm inline-block rounded-full p-1'>
|
|
<RiArrowLeftLine size={12} />
|
|
</div>
|
|
<span className='system-xs-regular ml-2'>{t('login.back')}</span>
|
|
</div>
|
|
</div>
|
|
}
|