chang routing files palce

This commit is contained in:
valitovgaziz
2024-08-16 19:45:22 +05:00
parent 9da6db076b
commit 4b7af37296
9 changed files with 21 additions and 8 deletions
+48
View File
@@ -0,0 +1,48 @@
package auth
import (
"api/src/models"
"api/src/storages/psql"
"encoding/json"
"net/http"
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
)
func Register(w http.ResponseWriter, r *http.Request) {
var Crenetials models.Crenetials
// Decoe body
if err := json.NewDecoder(r.Body).Decode(&Crenetials); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// shep password
hashedPassword, err := hashPassword(Crenetials.Password)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
id := uuid.New()
user := models.User{
Id: id,
Name: Crenetials.Name,
Email: Crenetials.Email,
Password: hashedPassword,
Phone: Crenetials.Phone,
}
if result := psql.PSQL_GORM_DB.Create(&user); result.Error != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}
func hashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes), err
}