Skip to content

Commit

Permalink
Merge pull request #233 from FutabaRio/fix-register
Browse files Browse the repository at this point in the history
fix: 修改了用户注册相关 #231
  • Loading branch information
guansss authored Nov 24, 2023
2 parents 570b35f + 20ba10b commit 202939a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 11 deletions.
16 changes: 16 additions & 0 deletions src/apis/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,35 @@ export interface RegisterResponse {}

export const requestRegister = (
email: string,
registrationToken: string,
username: string,
password: string,
) => {
return jsonRequest<Response<RegisterResponse>>('/user/register', {
method: 'POST',
json: {
email,
registration_token: registrationToken,
user_name: username,
password,
},
})
}

export interface EmailToenResponse {}

export const reqeustRegistrationToken = (email: string) => {
return jsonRequest<Response<EmailToenResponse>>(
'/user/sendRegistrationToken',
{
method: 'POST',
json: {
email,
},
},
)
}

export interface UpdateInfoResponse {}

export const requestUpdateInfo = ({
Expand Down
43 changes: 41 additions & 2 deletions src/components/account/AuthFormShared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { FormField, FormFieldProps } from 'components/FormField'
import { REGEX_EMAIL } from 'utils/regexes'

export type RuleKeys = 'email' | 'password' | 'username'
export type RuleKeys = 'email' | 'password' | 'username' | 'registertoken'

export const rule: Record<RuleKeys, UseControllerProps['rules']> = {
email: {
Expand All @@ -26,6 +26,11 @@ export const rule: Record<RuleKeys, UseControllerProps['rules']> = {
minLength: { value: 4, message: '用户名长度不能小于 4 位' },
maxLength: { value: 24, message: '用户名长度不能大于 24 位' },
},
registertoken: {
required: '邮箱验证码为必填项',
minLength: { value: 6, message: '邮箱验证码长度为 6 位' },
maxLength: { value: 6, message: '邮箱验证码长度为 6 位' },
},
}

// --- **Opinioned** AuthForm Field Components ---
Expand Down Expand Up @@ -73,7 +78,41 @@ export const AuthFormEmailField = <T extends FieldValues>({
),
}}
FormGroupProps={{
helperText: register && '邮箱将需要链接确认',
helperText: register && '将通过发送邮件输入验证码确认',
}}
/>
)
}
export const AuthRegistrationTokenField = <T extends FieldValues>({
label = '邮箱验证码',
control,
error,
field,
register,
autoComplete = '',
inputGroupProps,
}: AuthFormFieldProps<T>) => {
return (
<FormField
label={label}
field={field}
control={control}
error={error}
ControllerProps={{
rules: rule.registertoken,
render: (renderProps) => (
<InputGroup
id={field}
placeholder="123456"
autoComplete={autoComplete}
{...renderProps.field}
value={renderProps.field.value || ''}
{...inputGroupProps?.(renderProps)}
/>
),
}}
FormGroupProps={{
helperText: register && '请输入邮件中的验证码',
}}
/>
)
Expand Down
10 changes: 6 additions & 4 deletions src/components/account/EditDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const EditDialog: FC<EditDialogProps> = ({ isOpen, onClose }) => {
<span className="ml-1">账户信息</span>
</div>
}
panel={<InfoPanel />}
panel={<InfoPanel onClose={onClose} />}
/>
<Tab
id="password"
Expand All @@ -61,7 +61,7 @@ export const EditDialog: FC<EditDialogProps> = ({ isOpen, onClose }) => {
<span className="ml-1">密码</span>
</div>
}
panel={<PasswordPanel />}
panel={<PasswordPanel onClose={onClose} />}
/>
</Tabs>
</GlobalErrorBoundary>
Expand All @@ -70,7 +70,7 @@ export const EditDialog: FC<EditDialogProps> = ({ isOpen, onClose }) => {
)
}

const InfoPanel = () => {
const InfoPanel = ({ onClose }) => {
interface FormValues {
username: string
}
Expand Down Expand Up @@ -109,6 +109,7 @@ const InfoPanel = () => {
intent: 'success',
message: `更新成功`,
})
onClose(false)
} catch (e) {
console.warn(e)
setError('global' as any, { message: formatError(e) })
Expand Down Expand Up @@ -149,7 +150,7 @@ const InfoPanel = () => {
)
}

const PasswordPanel = () => {
const PasswordPanel = ({ onClose }) => {
interface FormValues {
original: string
newPassword: string
Expand Down Expand Up @@ -182,6 +183,7 @@ const PasswordPanel = () => {
intent: 'success',
message: `更新成功`,
})
onClose(false)
} catch (e) {
console.warn(e)
setError('global' as any, { message: formatError(e) })
Expand Down
73 changes: 68 additions & 5 deletions src/components/account/RegisterPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { Button } from '@blueprintjs/core'

import { requestRegister } from 'apis/auth'
import { FC } from 'react'
import { reqeustRegistrationToken, requestRegister } from 'apis/auth'
import { FC, useEffect, useState } from 'react'
import { useForm } from 'react-hook-form'

import { AppToaster } from 'components/Toaster'
import { NetworkError } from 'utils/fetcher'
import { REGEX_EMAIL } from 'utils/regexes'
import { wrapErrorMessage } from 'utils/wrapErrorMessage'

import {
AuthFormEmailField,
AuthFormPasswordField,
AuthFormUsernameField,
AuthRegistrationTokenField,
} from './AuthFormShared'

export interface RegisterFormValues {
email: string
password: string
username: string
registrationToken: string
}

export const RegisterPanel: FC<{
Expand All @@ -27,20 +30,60 @@ export const RegisterPanel: FC<{
control,
handleSubmit,
formState: { errors, isValid, isDirty, isSubmitting },
getValues,
} = useForm<RegisterFormValues>()

const [isSendEmailButtonDisabled, setSendEmailButtonDisabled] =
useState(false)
const [countdown, setCountdown] = useState(60)
const onSubmit = async (val: RegisterFormValues) => {
await wrapErrorMessage(
(e: NetworkError) => `注册失败:${e.message}`,
requestRegister(val.email, val.username, val.password),
requestRegister(
val.email,
val.registrationToken,
val.username,
val.password,
),
)
AppToaster.show({
intent: 'success',
message: `已向注册邮箱发送验证邮件,请使用邮件内的验证链接进行验证`,
message: `注册成功`,
})
onComplete()
}
const handleCountdownTick = () => {
setCountdown((prevCountdown) => prevCountdown - 1)
}
useEffect(() => {
let countdownInterval
if (countdown <= 0) {
setCountdown(60)
setSendEmailButtonDisabled(false)
} else if (isSendEmailButtonDisabled) {
countdownInterval = setInterval(handleCountdownTick, 1000)
}
return () => clearInterval(countdownInterval)
}, [isSendEmailButtonDisabled, countdown])

const onEmailSubmit = async () => {
const val = getValues()
if (!REGEX_EMAIL.test(val.email)) {
AppToaster.show({
intent: 'danger',
message: `邮箱输入为空或格式错误,请重新输入`,
})
return
}
await wrapErrorMessage(
(e: NetworkError) => `发送失败:${e.message}`,
reqeustRegistrationToken(val.email),
)
AppToaster.show({
intent: 'success',
message: `邮件发送成功`,
})
setSendEmailButtonDisabled(true)
}
return (
<form onSubmit={handleSubmit(onSubmit)}>
<AuthFormEmailField
Expand All @@ -49,6 +92,26 @@ export const RegisterPanel: FC<{
error={errors.email}
field="email"
/>
<div className="mt-6 flex justify-end">
<Button
disabled={
(!isValid && !isDirty) || isSubmitting || isSendEmailButtonDisabled
}
intent="primary"
type="button"
icon="envelope"
className="self-stretch"
onClick={onEmailSubmit}
>
{isSendEmailButtonDisabled ? `${countdown} 秒再试` : '发送验证码'}
</Button>
</div>
<AuthRegistrationTokenField
register
control={control}
error={errors.registrationToken}
field="registrationToken"
/>

<AuthFormUsernameField
control={control}
Expand Down

0 comments on commit 202939a

Please sign in to comment.