diff --git a/src/business/login/login.service.spec.ts b/src/business/login/login.service.spec.ts index 59e1e6c..f3ac346 100644 --- a/src/business/login/login.service.spec.ts +++ b/src/business/login/login.service.spec.ts @@ -20,6 +20,7 @@ describe('LoginService', () => { github_id: null as string | null, avatar_url: null as string | null, role: 1, + email_verified: false, created_at: new Date(), updated_at: new Date() }; diff --git a/src/business/login/login.service.ts b/src/business/login/login.service.ts index 19fb510..dcc1ad0 100644 --- a/src/business/login/login.service.ts +++ b/src/business/login/login.service.ts @@ -287,6 +287,108 @@ export class LoginService { } } + /** + * 发送邮箱验证码 + * + * @param email 邮箱地址 + * @returns 响应结果 + */ + async sendEmailVerification(email: string): Promise> { + try { + this.logger.log(`发送邮箱验证码: ${email}`); + + // 调用核心服务发送验证码 + const verificationCode = await this.loginCoreService.sendEmailVerification(email); + + this.logger.log(`邮箱验证码已发送: ${email}`); + + // 实际应用中不应返回验证码,这里仅用于演示 + return { + success: true, + data: { verification_code: verificationCode }, + message: '验证码已发送,请查收邮件' + }; + } catch (error) { + this.logger.error(`发送邮箱验证码失败: ${email}`, error instanceof Error ? error.stack : String(error)); + + return { + success: false, + message: error instanceof Error ? error.message : '发送验证码失败', + error_code: 'SEND_EMAIL_VERIFICATION_FAILED' + }; + } + } + + /** + * 验证邮箱验证码 + * + * @param email 邮箱地址 + * @param code 验证码 + * @returns 响应结果 + */ + async verifyEmailCode(email: string, code: string): Promise { + try { + this.logger.log(`验证邮箱验证码: ${email}`); + + // 调用核心服务验证验证码 + const isValid = await this.loginCoreService.verifyEmailCode(email, code); + + if (isValid) { + this.logger.log(`邮箱验证成功: ${email}`); + return { + success: true, + message: '邮箱验证成功' + }; + } else { + return { + success: false, + message: '验证码错误', + error_code: 'INVALID_VERIFICATION_CODE' + }; + } + } catch (error) { + this.logger.error(`邮箱验证失败: ${email}`, error instanceof Error ? error.stack : String(error)); + + return { + success: false, + message: error instanceof Error ? error.message : '邮箱验证失败', + error_code: 'EMAIL_VERIFICATION_FAILED' + }; + } + } + + /** + * 重新发送邮箱验证码 + * + * @param email 邮箱地址 + * @returns 响应结果 + */ + async resendEmailVerification(email: string): Promise> { + try { + this.logger.log(`重新发送邮箱验证码: ${email}`); + + // 调用核心服务重新发送验证码 + const verificationCode = await this.loginCoreService.resendEmailVerification(email); + + this.logger.log(`邮箱验证码已重新发送: ${email}`); + + // 实际应用中不应返回验证码,这里仅用于演示 + return { + success: true, + data: { verification_code: verificationCode }, + message: '验证码已重新发送,请查收邮件' + }; + } catch (error) { + this.logger.error(`重新发送邮箱验证码失败: ${email}`, error instanceof Error ? error.stack : String(error)); + + return { + success: false, + message: error instanceof Error ? error.message : '重新发送验证码失败', + error_code: 'RESEND_EMAIL_VERIFICATION_FAILED' + }; + } + } + /** * 格式化用户信息 *