From b845b82ee0b40d42fb7fe7cfbd5f120c46fae454 Mon Sep 17 00:00:00 2001 From: valitovgaziz Date: Mon, 12 Aug 2024 09:40:55 +0500 Subject: [PATCH] register save handler is done --- api/src/auth/jwt.go | 39 ++++++++++++++++++++++++++-- api/src/initializers/initializers.go | 5 ++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/api/src/auth/jwt.go b/api/src/auth/jwt.go index a2a61a9..b66d4a1 100644 --- a/api/src/auth/jwt.go +++ b/api/src/auth/jwt.go @@ -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) { - + } \ No newline at end of file diff --git a/api/src/initializers/initializers.go b/api/src/initializers/initializers.go index f670f6b..1b72730 100644 --- a/api/src/initializers/initializers.go +++ b/api/src/initializers/initializers.go @@ -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