forked from datawhale/whale-town-end
service:更新登录业务服务支持邮箱验证
- 添加发送邮箱验证码服务方法 - 添加验证邮箱验证码服务方法 - 添加重新发送邮箱验证码服务方法 - 集成验证码服务和邮件服务 - 更新相关的单元测试
This commit is contained in:
@@ -20,6 +20,7 @@ describe('LoginService', () => {
|
|||||||
github_id: null as string | null,
|
github_id: null as string | null,
|
||||||
avatar_url: null as string | null,
|
avatar_url: null as string | null,
|
||||||
role: 1,
|
role: 1,
|
||||||
|
email_verified: false,
|
||||||
created_at: new Date(),
|
created_at: new Date(),
|
||||||
updated_at: new Date()
|
updated_at: new Date()
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -287,6 +287,108 @@ export class LoginService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送邮箱验证码
|
||||||
|
*
|
||||||
|
* @param email 邮箱地址
|
||||||
|
* @returns 响应结果
|
||||||
|
*/
|
||||||
|
async sendEmailVerification(email: string): Promise<ApiResponse<{ verification_code?: string }>> {
|
||||||
|
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<ApiResponse> {
|
||||||
|
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<ApiResponse<{ verification_code?: string }>> {
|
||||||
|
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'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化用户信息
|
* 格式化用户信息
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user