@@ -4,6 +4,7 @@ import { compare, hash } from 'bcrypt'
44import { HanaException } from '@/common/exceptions/hana.exception'
55import { DEFAULT_COOKIE_MAX_AGE , REMEMBER_ME_COOKIE_MAX_AGE } from '@/constants/cookie'
66import { RoleName } from '@/constants/role'
7+ import { EmailCodeService } from '@/email-code/email-code.service'
78import { PrismaService } from '@/infra/prisma/prisma.service'
89import { SystemConfigService } from '@/infra/system-config/system-config.service'
910import { SessionService } from '@/session/session.service'
@@ -18,6 +19,7 @@ export class AuthService {
1819 private readonly prisma : PrismaService ,
1920 private readonly sessionService : SessionService ,
2021 private readonly systemConfigService : SystemConfigService ,
22+ private readonly emailCodeService : EmailCodeService ,
2123 ) { }
2224
2325 /** 对密码进行哈希处理 */
@@ -209,14 +211,25 @@ export class AuthService {
209211
210212 /** 用户注册 */
211213 async register ( dto : RegisterReqDto ) {
212- const registerEnabled = this . systemConfigService . get < boolean > ( SystemConfigKey . REGISTER_ENABLED )
213- if ( ! registerEnabled ) {
214- throw new HanaException ( 'REGISTER_DISABLED' )
215- }
216-
217- const { email, username, name, password, confirmPassword } = dto
214+ const { email, verificationCode, username, name, password, confirmPassword } = dto
218215
219216 try {
217+ // 系统配置校验
218+ // 1. 是否允许注册
219+ const registerEnabled = this . systemConfigService . get < boolean > ( SystemConfigKey . REGISTER_ENABLED )
220+ if ( ! registerEnabled ) {
221+ throw new HanaException ( 'REGISTER_DISABLED' )
222+ }
223+
224+ // 2. 是否需要邮箱验证
225+ const registerEmailVerify = this . systemConfigService . get < boolean > ( SystemConfigKey . REGISTER_EMAIL_VERIFY )
226+ if ( registerEmailVerify ) {
227+ if ( ! verificationCode ) {
228+ throw new HanaException ( 'REGISTER_EMAIL_VERIFY_REQUIRED' )
229+ }
230+ await this . emailCodeService . verifyEmailCode ( email , verificationCode )
231+ }
232+
220233 // 验证密码确认
221234 if ( password !== confirmPassword ) {
222235 throw new HanaException ( 'PASSWORD_MISMATCH' )
0 commit comments