feat: add password recovery flow (#358)
This commit is contained in:
1 parent
8ebceecbb6
commit
e124a9ea04
19 files changed
+875
-52
No files matched your search
@@ -26,6 +26,8 @@ var ErrTextReplayQuotaExceeded = errors.New("text replay quota exceeded")
|
||||
|
||||
var ErrTextReplayClosed = errors.New("text replay task is closed")
|
||||
|
||||
var ErrEmailVerificationCodeInvalid = errors.New("email verification code is no longer valid")
|
||||
|
||||
var ErrProjectAssetFolderNotEmpty = errors.New("project asset folder is not empty")
|
||||
|
||||
var ErrProjectHasActiveTasks = errors.New("project has active tasks")
|
||||
@@ -307,6 +309,36 @@ func (r *Repository) CreateUserWithEmailVerification(user *model.User, verificat
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Repository) ResetUserPasswordWithEmailVerification(userID string, email string, purpose string, verificationCodeID string, passwordHash string, usedAt time.Time) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
codeResult := tx.Model(&model.EmailVerificationCode{}).
|
||||
Where("id = ? AND email = ? AND purpose = ? AND used_at IS NULL AND expires_at > ?", verificationCodeID, email, purpose, usedAt).
|
||||
Update("used_at", usedAt)
|
||||
if codeResult.Error != nil {
|
||||
return codeResult.Error
|
||||
}
|
||||
if codeResult.RowsAffected != 1 {
|
||||
return ErrEmailVerificationCodeInvalid
|
||||
}
|
||||
|
||||
userResult := tx.Model(&model.User{}).
|
||||
Where("id = ? AND email <> '' AND lower(email) = lower(?) AND status = ? AND password_hash <> ''", userID, email, model.UserStatusActive).
|
||||
Updates(map[string]any{"password_hash": passwordHash, "updated_at": usedAt})
|
||||
if userResult.Error != nil {
|
||||
return userResult.Error
|
||||
}
|
||||
if userResult.RowsAffected != 1 {
|
||||
return ErrEmailVerificationCodeInvalid
|
||||
}
|
||||
if err := tx.Delete(&model.AuthSession{}, "user_id = ?", userID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Model(&model.EmailVerificationCode{}).
|
||||
Where("email = ? AND purpose = ? AND used_at IS NULL", email, purpose).
|
||||
Update("used_at", usedAt).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteExpiredEmailVerificationCodes(now time.Time) error {
|
||||
return r.db.Delete(&model.EmailVerificationCode{}, "expires_at <= ? OR used_at IS NOT NULL", now).Error
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user