Explorar el Código

feat: able to use email to login (close #343,#348)

CalciumIon hace 1 año
padre
commit
45b9de9df9
Se han modificado 1 ficheros con 7 adiciones y 2 borrados
  1. 7 2
      model/user.go

+ 7 - 2
model/user.go

@@ -295,10 +295,15 @@ func (user *User) ValidateAndFill() (err error) {
 	// that means if your field’s value is 0, '', false or other zero values,
 	// that means if your field’s value is 0, '', false or other zero values,
 	// it won’t be used to build query conditions
 	// it won’t be used to build query conditions
 	password := user.Password
 	password := user.Password
-	if user.Username == "" || password == "" {
+	if (user.Username == "" && user.Email == "") || password == "" {
 		return errors.New("用户名或密码为空")
 		return errors.New("用户名或密码为空")
 	}
 	}
-	DB.Where(User{Username: user.Username}).First(user)
+	// find buy username or email
+	if user.Username != "" {
+		DB.Where(User{Username: user.Username}).First(user)
+	} else if user.Email != "" {
+		DB.Where(User{Email: user.Email}).First(user)
+	}
 	okay := common.ValidatePasswordAndHash(password, user.Password)
 	okay := common.ValidatePasswordAndHash(password, user.Password)
 	if !okay || user.Status != common.UserStatusEnabled {
 	if !okay || user.Status != common.UserStatusEnabled {
 		return errors.New("用户名或密码错误,或用户已被封禁")
 		return errors.New("用户名或密码错误,或用户已被封禁")