fusenapi/utils/auth/register.go

77 lines
2.0 KiB
Go
Raw Normal View History

2023-07-14 11:26:00 +00:00
package auth
import (
"net/mail"
)
// ValidateEmail checks if the provided string is a valid email address.
func ValidateEmail(email string) bool {
_, err := mail.ParseAddress(email)
return err == nil
}
// ValidatePassword checks if the provided password is strong enough.
// In this example, we just check if the password length is 8 or more.
func ValidatePassword(password string) bool {
const minPasswordLength = 8
return len(password) >= minPasswordLength
}
var secret = []byte("your-secret")
// func generateConfirmationLink(id, email, password, name string, platform string) (string, error) {
// // 创建一个新的 JWT并将用户的电子邮件设置为它的主题。
// token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
// "email": email,
// "password": password,
// "id": id,
// "platform": platform,
// "exp": time.Now().Add(24 * time.Hour).Unix(), // Token expires after 24 hours
// })
// // 签署 JWT。
// tokenString, err := token.SignedString(secret)
// if err != nil {
// return "", err
// }
// // 生成确认链接,这个链接包含 JWT。
// link := url.URL{
// Scheme: "http",
// Host: "yourserver.com",
// Path: "/confirm",
// RawQuery: url.Values{
// "token": []string{tokenString},
// }.Encode(),
// }
// return link.String(), nil
// }
// func handleConfirm(w http.ResponseWriter, r *http.Request) {
// // 从请求中获取 JWT。
// tokenString := r.URL.Query().Get("token")
// // 解析和验证 JWT。
// token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
// return secret, nil
// })
// if err != nil || !token.Valid {
// http.Error(w, "Invalid confirmation link", http.StatusBadRequest)
// return
// }
// claims, ok := token.Claims.(jwt.MapClaims)
// if !ok || !token.Valid {
// http.Error(w, "Invalid token", http.StatusBadRequest)
// return
// }
// email := claims["sub"].(string)
// // 确认链接有效,可以创建用户账号了。
// createUser(email)
// }