Skip to content

Instantly share code, notes, and snippets.

@dipeshhkc
Last active May 22, 2021 10:32
Show Gist options
  • Save dipeshhkc/7f03be13a7225cf1a30940c1daea6be9 to your computer and use it in GitHub Desktop.
Save dipeshhkc/7f03be13a7225cf1a30940c1daea6be9 to your computer and use it in GitHub Desktop.
func hashPassword(pass *string) {
bytePass := []byte(*pass)
hPass, _ := bcrypt.GenerateFromPassword(bytePass, bcrypt.DefaultCost)
*pass = string(hPass)
}
func comparePassword(dbPass, pass string) bool {
return bcrypt.CompareHashAndPassword([]byte(dbPass), []byte(pass)) == nil
}
func (h *userHandler) SignInUser(ctx *gin.Context) {
var user model.User
if err := ctx.ShouldBindJSON(&user); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
dbUser, err := h.repo.GetByEmail(user.Email)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"msg": "No Such User Found"})
return
}
if isTrue := comparePassword(dbUser.Password, user.Password); isTrue {
fmt.Println("user before", dbUser.ID)
token := GenerateToken(dbUser.ID)
ctx.JSON(http.StatusOK, gin.H{"msg": "Successfully SignIN", "token": token})
return
}
ctx.JSON(http.StatusInternalServerError, gin.H{"msg": "Password not matched"})
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment