register save handler is done
This commit is contained in:
+37
-2
@@ -1,11 +1,46 @@
|
|||||||
package auth
|
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) {
|
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) {
|
func Login(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -18,7 +18,6 @@ import (
|
|||||||
"github.com/go-chi/chi/v5/middleware"
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
var Done = make(chan bool)
|
var Done = make(chan bool)
|
||||||
|
|
||||||
func InitDBconnection() {
|
func InitDBconnection() {
|
||||||
@@ -56,7 +55,7 @@ func InitChiRouting() {
|
|||||||
slog.Info("Init routing")
|
slog.Info("Init routing")
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
|
|
||||||
// middlewares
|
// middlewares
|
||||||
r.Use(middleware.Logger)
|
r.Use(middleware.Logger)
|
||||||
r.Use(middleware.Timeout(60 * time.Second))
|
r.Use(middleware.Timeout(60 * time.Second))
|
||||||
r.Use(middleware.RequestID)
|
r.Use(middleware.RequestID)
|
||||||
@@ -71,7 +70,7 @@ func InitChiRouting() {
|
|||||||
|
|
||||||
r.Route("/auth", func(r chi.Router) {
|
r.Route("/auth", func(r chi.Router) {
|
||||||
r.Post("/register", auth.Register)
|
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
|
// up server on os.Getenv("SERVER_PORT") port on gorutin
|
||||||
|
|||||||
Reference in New Issue
Block a user