Skip to content

Instantly share code, notes, and snippets.

@linux08
Created May 4, 2019 11:21
Show Gist options
  • Save linux08/becc1276681045296536e83867b224a0 to your computer and use it in GitHub Desktop.
Save linux08/becc1276681045296536e83867b224a0 to your computer and use it in GitHub Desktop.
import (
"auth/models"
"auth/utils"
"encoding/json"
"fmt"
"net/http"
"golang.org/x/crypto/bcrypt"
)
type ErrorResponse struct {
Err string
}
type error interface {
Error() string
}
var db = utils.ConnectDB()
func CreateUser(w http.ResponseWriter, r *http.Request) {
user := &models.User{}
json.NewDecoder(r.Body).Decode(user)
pass, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
fmt.Println(err)
err := ErrorResponse{
Err: "Password Encryption failed",
}
json.NewEncoder(w).Encode(err)
}
user.Password = string(pass)
createdUser := db.Create(user)
var errMessage = createdUser.Error
if createdUser.Error != nil {
fmt.Println(errMessage)
}
json.NewEncoder(w).Encode(createdUser)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment