mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-26 11:06:23 +00:00
feat: add email verification and password reset functionality
- Introduced environment variables for database and email configurations. - Implemented email verification code generation and validation. - Added password reset feature with email verification. - Updated user registration and profile management APIs. - Refactored user security settings to include email and password updates. - Enhanced console layout with internationalization support. - Removed deprecated settings page and integrated global settings. - Added new reset password page and form components. - Updated localization files for new features and translations.
This commit is contained in:
@ -3,9 +3,12 @@ package utils
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gopkg.in/gomail.v2"
|
||||
"html/template"
|
||||
|
||||
"github.com/snowykami/neo-blog/pkg/constant"
|
||||
"gopkg.in/gomail.v2"
|
||||
)
|
||||
|
||||
type emailUtils struct{}
|
||||
@ -42,7 +45,7 @@ func (e *emailUtils) SendTemplate(emailConfig *EmailConfig, target, subject, htm
|
||||
// SendEmail 使用gomail库发送邮件
|
||||
func (e *emailUtils) SendEmail(emailConfig *EmailConfig, target, subject, content string, isHTML bool) error {
|
||||
if !emailConfig.Enable {
|
||||
return nil
|
||||
return errors.New("邮箱服务未启用")
|
||||
}
|
||||
// 创建新邮件
|
||||
m := gomail.NewMessage()
|
||||
@ -73,12 +76,12 @@ func (e *emailUtils) SendEmail(emailConfig *EmailConfig, target, subject, conten
|
||||
|
||||
func (e *emailUtils) GetEmailConfigFromEnv() *EmailConfig {
|
||||
return &EmailConfig{
|
||||
Enable: Env.GetAsBool("EMAIL_ENABLE", false),
|
||||
Username: Env.Get("EMAIL_USERNAME", ""),
|
||||
Address: Env.Get("EMAIL_ADDRESS", ""),
|
||||
Host: Env.Get("EMAIL_HOST", "smtp.example.com"),
|
||||
Port: Env.GetAsInt("EMAIL_PORT", 587),
|
||||
Password: Env.Get("EMAIL_PASSWORD", ""),
|
||||
SSL: Env.GetAsBool("EMAIL_SSL", true),
|
||||
Enable: Env.GetAsBool(constant.EnvKeyEmailEnable, false),
|
||||
Username: Env.Get(constant.EnvKeyEmailUsername, ""),
|
||||
Address: Env.Get(constant.EnvKeyEmailAddress, ""),
|
||||
Host: Env.Get(constant.EnvKeyEmailHost, "smtp.example.com"),
|
||||
Port: Env.GetAsInt(constant.EnvKeyEmailPort, 587),
|
||||
Password: Env.Get(constant.EnvKeyEmailPassword, ""),
|
||||
SSL: Env.GetAsBool(constant.EnvKeyEmailSsl, true),
|
||||
}
|
||||
}
|
||||
|
@ -1 +1,27 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/snowykami/neo-blog/pkg/constant"
|
||||
)
|
||||
|
||||
func RequestEmailVerify(email string) string {
|
||||
generatedVerificationCode := Strings.GenerateRandomStringWithCharset(6, "0123456789abcdef")
|
||||
kv := KV.GetInstance()
|
||||
kv.Set(constant.KVKeyEmailVerificationCode+email, generatedVerificationCode, time.Minute*10)
|
||||
return generatedVerificationCode
|
||||
}
|
||||
|
||||
func VerifyEmailCode(email, code string) bool {
|
||||
kv := KV.GetInstance()
|
||||
storedCode, ok := kv.Get(constant.KVKeyEmailVerificationCode + email)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if storedCode != code {
|
||||
return false
|
||||
}
|
||||
kv.Delete(constant.KVKeyEmailVerificationCode + email)
|
||||
return true
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package utils
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
|
Reference in New Issue
Block a user