Well, I'm learning to program in the GO language, and I made an api with a login system.
func Authenticate(db *gorm.DB, auth models.UserAuthForm) uint {
hasher.Write([]byte(auth.Password))
pass_hash := hex.EncodeToString(hasher.Sum(nil))
user := models.User{}
db.Select("id").Where("pass_hash = ? AND email = ?", pass_hash, auth.Email).First(&user)
hasher.Reset()
return user.ID
}
This function is called inside a controller with POST method in login is represented by Struct:
type UserAuthForm struct {
Email string 'json:"email" form:"email" binding:"required"'
Password string 'json:"password" form:"password" binding:"required"'
}
this uid represents the returned id and a cookie is saved in the Session
uid := crud.Authenticate(db, login)
if uid != 0 {
sess := sessions.Default(c)
sess.Set("user", crud.SaveCookieId(db, uid))
sess.Save()
c.JSON(200, gin.H{"Message": "Success!"})
return
}
c.JSON(401, gin.H{"Error": "Failed!"})
return
My real problem is that when I try to login, first the return is:
{
"Message": "Success!"
}
however when trying again, it returns:
{
"Error": "Failed!"
}
And if I try again, it succeeds again and so stays merged.