register save handler is done

This commit is contained in:
valitovgaziz
2024-08-12 09:40:55 +05:00
parent c8bfe07fb3
commit b845b82ee0
2 changed files with 39 additions and 5 deletions
+37 -2
View File
@@ -1,11 +1,46 @@
package auth
import "net/http"
import (
"api/src/models"
"api/src/storages/psql"
"encoding/json"
"net/http"
"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
}
user := models.User{Email: Crenetials.Email, Password: hashedPassword}
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
}
func checkPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
func Login(w http.ResponseWriter, r *http.Request) {
}
+2 -3
View File
@@ -18,7 +18,6 @@ import (
"github.com/go-chi/chi/v5/middleware"
)
var Done = make(chan bool)
func InitDBconnection() {
@@ -56,7 +55,7 @@ func InitChiRouting() {
slog.Info("Init routing")
r := chi.NewRouter()
// middlewares
// middlewares
r.Use(middleware.Logger)
r.Use(middleware.Timeout(60 * time.Second))
r.Use(middleware.RequestID)
@@ -71,7 +70,7 @@ func InitChiRouting() {
r.Route("/auth", func(r chi.Router) {
r.Post("/register", auth.Register)
r.Post("/loging", auth.Login)
r.Post("/login", auth.Login)
})
// up server on os.Getenv("SERVER_PORT") port on gorutin