add forget password
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type LoginRequest struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
@@ -10,6 +16,63 @@ type ResetPasswordRequest struct {
|
||||
}
|
||||
|
||||
type ResetPasswordChangeRequest struct {
|
||||
Token string `json:"token" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
OldPassword string `json:"old_password" validate:"required"`
|
||||
NewPassword string `json:"new_password" validate:"required,strongpwd"`
|
||||
}
|
||||
|
||||
func (e *ResetPasswordChangeRequest) Validate() error {
|
||||
validate := validator.New()
|
||||
|
||||
validate.RegisterValidation("strongpwd", validateStrongPassword)
|
||||
|
||||
if err := validate.Struct(e); err != nil {
|
||||
// Handle the validation errors
|
||||
for _, err := range err.(validator.ValidationErrors) {
|
||||
switch err.Field() {
|
||||
case "NewPassword":
|
||||
return fmt.Errorf("%w", validatePasswordError(err.Tag()))
|
||||
default:
|
||||
return fmt.Errorf("validation failed: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateStrongPassword(fl validator.FieldLevel) bool {
|
||||
password := fl.Field().String()
|
||||
|
||||
var (
|
||||
hasMinLen = len(password) >= 8
|
||||
)
|
||||
|
||||
return hasMinLen
|
||||
}
|
||||
|
||||
// Error messages for password validation
|
||||
var (
|
||||
ErrPasswordTooShort = errors.New("password must be at least 8 characters long")
|
||||
ErrPasswordNoUpper = errors.New("password must contain at least one uppercase letter")
|
||||
ErrPasswordNoLower = errors.New("password must contain at least one lowercase letter")
|
||||
ErrPasswordNoNumber = errors.New("password must contain at least one digit")
|
||||
ErrPasswordNoSpecial = errors.New("password must contain at least one special character (!@#$%^&*)")
|
||||
ErrPasswordValidation = errors.New("password does not meet the strength requirements")
|
||||
)
|
||||
|
||||
func validatePasswordError(tag string) error {
|
||||
switch tag {
|
||||
case "min":
|
||||
return ErrPasswordTooShort
|
||||
case "uppercase":
|
||||
return ErrPasswordNoUpper
|
||||
case "lowercase":
|
||||
return ErrPasswordNoLower
|
||||
case "number":
|
||||
return ErrPasswordNoNumber
|
||||
case "special":
|
||||
return ErrPasswordNoSpecial
|
||||
default:
|
||||
return ErrPasswordValidation
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user