feat: use system config enableEmailVerify with user regitser, reset password

This commit is contained in:
fantasticit 2022-08-17 18:10:46 +08:00
parent b5e2bea519
commit d96c63e008
7 changed files with 30 additions and 24 deletions

View File

@ -45,7 +45,7 @@ export const System = () => {
label={{ label={{
text: '邮箱检验', text: '邮箱检验',
extra: ( extra: (
<Tooltip content="开启邮箱检验后,新注册用户必须通过邮箱验证"> <Tooltip content="开启邮箱检验后,用户注册、密码等操作必须通过邮箱验证">
<IconHelpCircle style={{ color: '--semi-color-text-1' }} /> <IconHelpCircle style={{ color: '--semi-color-text-1' }} />
</Tooltip> </Tooltip>
), ),

View File

@ -1,5 +1,5 @@
import { Button, Col, Form, Row, Toast, Typography } from '@douyinfe/semi-ui'; import { Button, Col, Form, Row, Toast, Typography } from '@douyinfe/semi-ui';
import { useResetPassword, useVerifyCode } from 'data/user'; import { useResetPassword, useSystemPublicConfig, useVerifyCode } from 'data/user';
import { useInterval } from 'hooks/use-interval'; import { useInterval } from 'hooks/use-interval';
import { useToggle } from 'hooks/use-toggle'; import { useToggle } from 'hooks/use-toggle';
import React, { useCallback, useState } from 'react'; import React, { useCallback, useState } from 'react';
@ -9,6 +9,7 @@ export const ResetPassword = ({ onSuccess }) => {
const [hasSendVerifyCode, toggleHasSendVerifyCode] = useToggle(false); const [hasSendVerifyCode, toggleHasSendVerifyCode] = useToggle(false);
const [countDown, setCountDown] = useState(0); const [countDown, setCountDown] = useState(0);
const { reset, loading } = useResetPassword(); const { reset, loading } = useResetPassword();
const { data: systemConfig } = useSystemPublicConfig();
const { sendVerifyCode, loading: sendVerifyCodeLoading } = useVerifyCode(); const { sendVerifyCode, loading: sendVerifyCodeLoading } = useVerifyCode();
const onFormChange = useCallback((formState) => { const onFormChange = useCallback((formState) => {
@ -67,22 +68,24 @@ export const ResetPassword = ({ onSuccess }) => {
]} ]}
/> />
<Row gutter={8} style={{ paddingTop: 12 }}> {systemConfig && systemConfig.enableEmailVerify ? (
<Col span={16}> <Row gutter={8} style={{ paddingTop: 12 }}>
<Form.Input <Col span={16}>
noLabel <Form.Input
fieldStyle={{ paddingTop: 0 }} noLabel
placeholder={'请输入验证码'} fieldStyle={{ paddingTop: 0 }}
field="verifyCode" placeholder={'请输入验证码'}
rules={[{ required: true, message: '请输入邮箱收到的验证码!' }]} field="verifyCode"
/> rules={[{ required: true, message: '请输入邮箱收到的验证码!' }]}
</Col> />
<Col span={8}> </Col>
<Button disabled={!email || countDown > 0} loading={sendVerifyCodeLoading} onClick={getVerifyCode} block> <Col span={8}>
{hasSendVerifyCode ? countDown : '获取验证码'} <Button disabled={!email || countDown > 0} loading={sendVerifyCodeLoading} onClick={getVerifyCode} block>
</Button> {hasSendVerifyCode ? countDown : '获取验证码'}
</Col> </Button>
</Row> </Col>
</Row>
) : null}
<Form.Input <Form.Input
noLabel noLabel

View File

@ -6,7 +6,7 @@ import { ResetPassword } from 'components/user/reset-password';
import { useRouterQuery } from 'hooks/use-router-query'; import { useRouterQuery } from 'hooks/use-router-query';
import Link from 'next/link'; import Link from 'next/link';
import Router from 'next/router'; import Router from 'next/router';
import React, { useCallback, useState } from 'react'; import React, { useCallback } from 'react';
import styles from './index.module.scss'; import styles from './index.module.scss';

View File

@ -9,7 +9,6 @@ import { useRouterQuery } from 'hooks/use-router-query';
import { useToggle } from 'hooks/use-toggle'; import { useToggle } from 'hooks/use-toggle';
import Link from 'next/link'; import Link from 'next/link';
import Router from 'next/router'; import Router from 'next/router';
import { emit } from 'process';
import React, { useCallback, useState } from 'react'; import React, { useCallback, useState } from 'react';
import styles from './index.module.scss'; import styles from './index.module.scss';

View File

@ -4,7 +4,7 @@ import { IsEmail, IsNotEmpty, IsOptional, IsString, MinLength } from 'class-vali
* *
*/ */
export class RegisterUserDto { export class RegisterUserDto {
@MinLength(5, { message: '用户账号至少5个字符' }) @MinLength(1, { message: '用户账号至少1个字符' })
@IsString({ message: '用户名称类型错误正确类型为String' }) @IsString({ message: '用户名称类型错误正确类型为String' })
@IsNotEmpty({ message: '用户账号不能为空' }) @IsNotEmpty({ message: '用户账号不能为空' })
name: string; name: string;

View File

@ -12,7 +12,7 @@ export class SystemEntity {
isSystemLocked: boolean; isSystemLocked: boolean;
/** /**
* *
*/ */
@Column({ type: 'boolean', default: false, comment: '是否启用邮箱校验' }) @Column({ type: 'boolean', default: false, comment: '是否启用邮箱校验' })
enableEmailVerify: boolean; enableEmailVerify: boolean;

View File

@ -185,7 +185,7 @@ export class UserService {
throw new HttpException('两次密码不一致,请重试', HttpStatus.BAD_REQUEST); throw new HttpException('两次密码不一致,请重试', HttpStatus.BAD_REQUEST);
} }
if (!(await this.verifyService.checkVerifyCode(email, verifyCode))) { if (currentSystemConfig.enableEmailVerify && !(await this.verifyService.checkVerifyCode(email, verifyCode))) {
throw new HttpException('验证码不正确,请检查', HttpStatus.BAD_REQUEST); throw new HttpException('验证码不正确,请检查', HttpStatus.BAD_REQUEST);
} }
@ -250,6 +250,7 @@ export class UserService {
* @returns * @returns
*/ */
async updateUser(user: UserEntity, dto: UpdateUserDto): Promise<IUser> { async updateUser(user: UserEntity, dto: UpdateUserDto): Promise<IUser> {
const currentSystemConfig = await this.systemService.getConfigFromDatabase();
const oldData = await this.userRepo.findOne(user.id); const oldData = await this.userRepo.findOne(user.id);
if (oldData.email !== dto.email) { if (oldData.email !== dto.email) {
@ -257,7 +258,10 @@ export class UserService {
throw new HttpException('该邮箱已被注册', HttpStatus.BAD_REQUEST); throw new HttpException('该邮箱已被注册', HttpStatus.BAD_REQUEST);
} }
if (!(await this.verifyService.checkVerifyCode(dto.email, dto.verifyCode))) { if (
currentSystemConfig.enableEmailVerify &&
!(await this.verifyService.checkVerifyCode(dto.email, dto.verifyCode))
) {
throw new HttpException('验证码不正确,请检查', HttpStatus.BAD_REQUEST); throw new HttpException('验证码不正确,请检查', HttpStatus.BAD_REQUEST);
} }
} }