changes for email signup

This commit is contained in:
onysd 2026-07-13 20:09:57 +03:00
parent 0fbfc8cd71
commit 3d9b0f1de7
15 changed files with 469 additions and 27 deletions

View file

@ -86,6 +86,10 @@ type Service struct {
loginEmailEnabled bool
loginEmailRequireSetup bool
loginEmailCodeLength int
// emailSignupEnabled 打开后888 前缀的合成号码domain.EncodeEmailPhone
// 在 sendCode 阶段直接解码出邮箱、复用登录邮箱同一投递通道发码,与
// loginEmailEnabled手机号账号的第二验证渠道是两条独立开关。
emailSignupEnabled bool
// premiumGrantMonths 是新注册账号默认赠送的会员月数0 表示关闭赠送。
premiumGrantMonths int
}
@ -181,6 +185,15 @@ func WithLoginEmail(opts LoginEmailOptions) Option {
}
}
// WithEmailSignup 打开「邮箱即身份」登录方式:见 emailSignupEnabled 字段注释。
// 邮件投递复用 WithLoginEmail 注入的 loginEmailSender调用方需保证两条配置
// 共用同一组 SMTP 设置时任一开关打开都会构造好 sender见 internal/config 校验)。
func WithEmailSignup(enabled bool) Option {
return func(s *Service) {
s.emailSignupEnabled = enabled
}
}
// NewService 创建登录服务。fixedCode 为开发固定验证码。
func NewService(users store.UserStore, auths store.AuthorizationStore, codes store.CodeStore, authKeys store.AuthKeyStore, tempKeys store.TempAuthKeyBindingStore, fixedCode string, opts ...Option) *Service {
s := &Service{users: users, auths: auths, codes: codes, authKeys: authKeys, tempKeys: tempKeys, fixedCode: fixedCode, codeTTL: 5 * time.Minute, codeMaxAttempts: 5, loginEmailCodeLength: 6}
@ -302,6 +315,11 @@ func (s *Service) SendCode(ctx context.Context, phone string) (string, error) {
if found {
issuedUserID = existing.ID
}
if s.emailSignupEnabled {
if email, ok := domain.DecodeEmailPhone(phone); ok {
return s.createEmailLoginCode(ctx, phone, email, issuedUserID)
}
}
if s.loginEmailEnabled && s.loginEmails != nil {
email, found, err := s.loginEmails.LoginEmailByPhone(ctx, phone)
if err != nil {