Skip to content

Instantly share code, notes, and snippets.

@dipeshhkc
Last active June 24, 2021 11:41
Show Gist options
  • Save dipeshhkc/d650ebe0e125e70919428bf817d4bd85 to your computer and use it in GitHub Desktop.
Save dipeshhkc/d650ebe0e125e70919428bf817d4bd85 to your computer and use it in GitHub Desktop.
//AddUser - Register a user
func (h userController) AddUser(enforcer *casbin.Enforcer) gin.HandlerFunc {
return func(ctx *gin.Context) {
var user model.User
if err := ctx.ShouldBindJSON(&user); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
utils.HashPassword(&user.Password)
user, err := h.userRepo.AddUser(user)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
enforcer.AddGroupingPolicy(fmt.Sprint(user.ID), user.Role)
user.Password = ""
ctx.JSON(http.StatusOK, user)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment