register save handler is done
This commit is contained in:
+36
-1
@@ -1,9 +1,44 @@
|
||||
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) {
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
|
||||
var Done = make(chan bool)
|
||||
|
||||
func InitDBconnection() {
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user