ChangePasswordForm.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 'use client'
  2. import { useCallback, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import useSWR from 'swr'
  5. import { useSearchParams } from 'next/navigation'
  6. import cn from 'classnames'
  7. import { CheckCircleIcon } from '@heroicons/react/24/solid'
  8. import Input from '../components/base/input'
  9. import Button from '@/app/components/base/button'
  10. import { changePasswordWithToken, verifyForgotPasswordToken } from '@/service/common'
  11. import Toast from '@/app/components/base/toast'
  12. import Loading from '@/app/components/base/loading'
  13. const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/
  14. const ChangePasswordForm = () => {
  15. const { t } = useTranslation()
  16. const searchParams = useSearchParams()
  17. const token = searchParams.get('token')
  18. const verifyTokenParams = {
  19. url: '/forgot-password/validity',
  20. body: { token },
  21. }
  22. const { data: verifyTokenRes, mutate: revalidateToken } = useSWR(verifyTokenParams, verifyForgotPasswordToken, {
  23. revalidateOnFocus: false,
  24. })
  25. const [password, setPassword] = useState('')
  26. const [confirmPassword, setConfirmPassword] = useState('')
  27. const [showSuccess, setShowSuccess] = useState(false)
  28. const showErrorMessage = useCallback((message: string) => {
  29. Toast.notify({
  30. type: 'error',
  31. message,
  32. })
  33. }, [])
  34. const valid = useCallback(() => {
  35. if (!password.trim()) {
  36. showErrorMessage(t('login.error.passwordEmpty'))
  37. return false
  38. }
  39. if (!validPassword.test(password)) {
  40. showErrorMessage(t('login.error.passwordInvalid'))
  41. return false
  42. }
  43. if (password !== confirmPassword) {
  44. showErrorMessage(t('common.account.notEqual'))
  45. return false
  46. }
  47. return true
  48. }, [password, confirmPassword, showErrorMessage, t])
  49. const handleChangePassword = useCallback(async () => {
  50. const token = searchParams.get('token') || ''
  51. if (!valid())
  52. return
  53. try {
  54. await changePasswordWithToken({
  55. url: '/forgot-password/resets',
  56. body: {
  57. token,
  58. new_password: password,
  59. password_confirm: confirmPassword,
  60. },
  61. })
  62. setShowSuccess(true)
  63. }
  64. catch {
  65. await revalidateToken()
  66. }
  67. }, [password, revalidateToken, token, valid])
  68. return (
  69. <div className={
  70. cn(
  71. 'flex flex-col items-center w-full grow justify-center',
  72. 'px-6',
  73. 'md:px-[108px]',
  74. )
  75. }>
  76. {!verifyTokenRes && <Loading />}
  77. {verifyTokenRes && !verifyTokenRes.is_valid && (
  78. <div className="flex flex-col md:w-[400px]">
  79. <div className="w-full mx-auto">
  80. <div className="mb-3 flex justify-center items-center w-20 h-20 p-5 rounded-[20px] border border-gray-100 shadow-lg text-[40px] font-bold">🤷‍♂️</div>
  81. <h2 className="text-[32px] font-bold text-gray-900">{t('login.invalid')}</h2>
  82. </div>
  83. <div className="w-full mx-auto mt-6">
  84. <Button variant='primary' className='w-full !text-sm'>
  85. <a href="https://dify.ai">{t('login.explore')}</a>
  86. </Button>
  87. </div>
  88. </div>
  89. )}
  90. {verifyTokenRes && verifyTokenRes.is_valid && !showSuccess && (
  91. <div className='flex flex-col md:w-[400px]'>
  92. <div className="w-full mx-auto">
  93. <h2 className="text-[32px] font-bold text-gray-900">
  94. {t('login.changePassword')}
  95. </h2>
  96. <p className='mt-1 text-sm text-gray-600'>
  97. {t('login.changePasswordTip')}
  98. </p>
  99. </div>
  100. <div className="w-full mx-auto mt-6">
  101. <div className="bg-white">
  102. {/* Password */}
  103. <div className='mb-5'>
  104. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  105. {t('common.account.newPassword')}
  106. </label>
  107. <Input
  108. id="password"
  109. type='password'
  110. value={password}
  111. onChange={e => setPassword(e.target.value)}
  112. placeholder={t('login.passwordPlaceholder') || ''}
  113. className='mt-1'
  114. />
  115. <div className='mt-1 text-xs text-text-secondary'>{t('login.error.passwordInvalid')}</div>
  116. </div>
  117. {/* Confirm Password */}
  118. <div className='mb-5'>
  119. <label htmlFor="confirmPassword" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  120. {t('common.account.confirmPassword')}
  121. </label>
  122. <Input
  123. id="confirmPassword"
  124. type='password'
  125. value={confirmPassword}
  126. onChange={e => setConfirmPassword(e.target.value)}
  127. placeholder={t('login.confirmPasswordPlaceholder') || ''}
  128. className='mt-1'
  129. />
  130. </div>
  131. <div>
  132. <Button
  133. variant='primary'
  134. className='w-full !text-sm'
  135. onClick={handleChangePassword}
  136. >
  137. {t('common.operation.reset')}
  138. </Button>
  139. </div>
  140. </div>
  141. </div>
  142. </div>
  143. )}
  144. {verifyTokenRes && verifyTokenRes.is_valid && showSuccess && (
  145. <div className="flex flex-col md:w-[400px]">
  146. <div className="w-full mx-auto">
  147. <div className="mb-3 flex justify-center items-center w-20 h-20 p-5 rounded-[20px] border border-gray-100 shadow-lg text-[40px] font-bold">
  148. <CheckCircleIcon className='w-10 h-10 text-[#039855]' />
  149. </div>
  150. <h2 className="text-[32px] font-bold text-gray-900">
  151. {t('login.passwordChangedTip')}
  152. </h2>
  153. </div>
  154. <div className="w-full mx-auto mt-6">
  155. <Button variant='primary' className='w-full'>
  156. <a href="/signin">{t('login.passwordChanged')}</a>
  157. </Button>
  158. </div>
  159. </div>
  160. )}
  161. </div>
  162. )
  163. }
  164. export default ChangePasswordForm