我有以下代码。

处理程序

func (authHandler *AuthHandler) Login(c *gin.Context) {
    var user models.User

    c.Bind(&user)

    if &user == nil {
        c.BindJSON(&user)
    }

    userObject, err := authHandler.userRepo.FindBy(
        models.User{
            Email: user.Email,
        },
    )

    if err != nil {
        c.JSON(401, gin.H{"_message": "User not found."})
        return
    }

    passErr := bcrypt.CompareHashAndPassword([]byte(userObject.Password), []byte(user.Password))

    if passErr != nil {
        c.JSON(401, gin.H{"_message": "Password incorrect."})
        return
    }

    token, err := services.CreateToken(userObject)

    if err != nil {
        c.JSON(401, gin.H{"_message": "Password incorrect."})
        return
    }

    c.JSON(200, gin.H{"token": token, "user": gin.H{
        "id":         &userObject.ID,
        "first_name": &userObject.FirstName,
        "last_name":  &userObject.LastName,
        "email":      &userObject.Email,
        "avatar":     &userObject.Avatar,
        "location":   &userObject.Location,
        "bg_img":     &userObject.BgImg,
    }})
}

模型
// user.go
type User struct {
    ID        string `gorm:"primary_key:true"`
    FirstName string `form:"first_name" json:"first_name,omitempty"`
    LastName  string `form:"last_name" json:"last_name,omitempty"`
    Password  string `form:"password" json:"password" bindind:"required"`
    Email     string `gorm:"type:varchar(110);unique_index" form:"email" json:"email,omitempty" binding:"required"`
    Location  string `form:"location" json:"location,omitempty"`
    Avatar    string `form:"avatar" json:"avatar,omitempty"`
    BgImg     string `form:"bg_img" json:"bg_img,omitempty"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt time.Time
}

func (repo *UserRepo) FindBy(params User) (User, error) {

    var user User

    err := repo.db.First(&user, "email = ?", params.Email).Error

    fmt.Println(err)

    if err != nil {
        return user, err
    }

    return user, nil
}

我尝试过用几种不同的方式进行查找,但是没有运气。我每次都会收到401响应,并且模型中的fmt.Prinlnt(err)显示record not found错误。

最佳答案

因此,事实证明我需要将DeletedAt字段设置为空值。在幕后,GORM自动检查DeletedAt值。即SELECT * FROM users WHERE email = '[email protected]' AND deleted_at IS NULL。但是,我的DeletedAt字段自动被设置为空白日期,从技术上讲不是NULL

我添加了一个结构...

type NullTime struct {
  time.Time
  Valid bool
}

然后更新我的模型...
type User struct {
    ID        string `gorm:"primary_key:true"`
    FirstName string `form:"first_name" json:"first_name,omitempty"`
    LastName  string `form:"last_name" json:"last_name,omitempty"`
    Password  string `form:"password" json:"password" bindind:"required"`
    Email     string `gorm:"type:varchar(110);unique_index" form:"email" json:"email,omitempty" binding:"required"`
    Location  string `form:"location" json:"location,omitempty"`
    Avatar    string `form:"avatar" json:"avatar,omitempty"`
    BgImg     string `form:"bg_img" json:"bg_img,omitempty"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt NullTime
}

09-27 11:40