From 568d09d0a2f459c05b67c5f653223bd4b4f5f275 Mon Sep 17 00:00:00 2001 From: valitovgaziz Date: Mon, 19 Aug 2024 21:42:02 +0500 Subject: [PATCH] recreate migration create table users; with unique constrains --- api/src/models/authDataStructs.go | 5 +++-- api/src/rt/auth/Login.go | 9 ++++++--- api/src/rt/auth/Registr.go | 12 ++++++------ .../20240813111847_create_extension_uuid_ossp.sql | 9 --------- ...ble.sql => 20240819162009_create_users_table.sql} | 10 +++++++--- 5 files changed, 22 insertions(+), 23 deletions(-) delete mode 100644 migrator/migrations/20240813111847_create_extension_uuid_ossp.sql rename migrator/migrations/{20240808094749_create_users_table.sql => 20240819162009_create_users_table.sql} (51%) diff --git a/api/src/models/authDataStructs.go b/api/src/models/authDataStructs.go index d6e604c..69e7f04 100644 --- a/api/src/models/authDataStructs.go +++ b/api/src/models/authDataStructs.go @@ -4,7 +4,7 @@ import ( "github.com/golang-jwt/jwt/v4" ) -type Crenetials struct { +type Credentials struct { Name string `json:"name"` Email string `json:"email"` Password string `json:"password"` @@ -14,4 +14,5 @@ type Crenetials struct { type Claims struct { jwt.RegisteredClaims Email string `json:"email"` -} + Phone string `json:"phone"` +} \ No newline at end of file diff --git a/api/src/rt/auth/Login.go b/api/src/rt/auth/Login.go index b8d1bca..f066511 100644 --- a/api/src/rt/auth/Login.go +++ b/api/src/rt/auth/Login.go @@ -15,14 +15,16 @@ import ( var jwtKey = []byte(os.Getenv("SECRET_KEY")) func Login(w http.ResponseWriter, r *http.Request) { - var creds models.Crenetials + var creds models.Credentials if err := json.NewDecoder(r.Body).Decode(&creds); err != nil { w.WriteHeader(http.StatusInternalServerError) return } // check user var user models.User - if result := psql.PSQL_GORM_DB.Where("email = ?", creds.Email).First(&user); result.Error != nil || !checkPasswordHash(creds.Password, user.Password) { + // get user by email + result := psql.PSQL_GORM_DB.Where("email = ?", creds.Email).First(&user) + if result.Error != nil || !checkPasswordHash(creds.Password, user.Password) { w.WriteHeader(http.StatusInternalServerError) return } @@ -34,6 +36,7 @@ func Login(w http.ResponseWriter, r *http.Request) { ExpiresAt: jwt.NewNumericDate(expirationtime), }, Email: user.Email, + Phone: user.Phone, } token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) @@ -54,4 +57,4 @@ func Login(w http.ResponseWriter, r *http.Request) { func checkPasswordHash(password, hash string) bool { err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) return err == nil -} \ No newline at end of file +} diff --git a/api/src/rt/auth/Registr.go b/api/src/rt/auth/Registr.go index c8748a8..e7ce904 100644 --- a/api/src/rt/auth/Registr.go +++ b/api/src/rt/auth/Registr.go @@ -12,15 +12,15 @@ import ( ) func Register(w http.ResponseWriter, r *http.Request) { - var Crenetials models.Crenetials + var Credentials models.Credentials // Decoe body - if err := json.NewDecoder(r.Body).Decode(&Crenetials); err != nil { + if err := json.NewDecoder(r.Body).Decode(&Credentials); err != nil { w.WriteHeader(http.StatusBadRequest) return } // shep password - hashedPassword, err := hashPassword(Crenetials.Password) + hashedPassword, err := hashPassword(Credentials.Password) if err != nil { w.WriteHeader(http.StatusInternalServerError) return @@ -30,10 +30,10 @@ func Register(w http.ResponseWriter, r *http.Request) { user := models.User{ Id: id, - Name: Crenetials.Name, - Email: Crenetials.Email, + Name: Credentials.Name, + Email: Credentials.Email, Password: hashedPassword, - Phone: Crenetials.Phone, + Phone: Credentials.Phone, } if result := psql.PSQL_GORM_DB.Create(&user); result.Error != nil { w.WriteHeader(http.StatusInternalServerError) diff --git a/migrator/migrations/20240813111847_create_extension_uuid_ossp.sql b/migrator/migrations/20240813111847_create_extension_uuid_ossp.sql deleted file mode 100644 index 5f0259a..0000000 --- a/migrator/migrations/20240813111847_create_extension_uuid_ossp.sql +++ /dev/null @@ -1,9 +0,0 @@ --- +goose Up --- +goose StatementBegin -CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; --- +goose StatementEnd - --- +goose Down --- +goose StatementBegin -DROP EXTENSION "uuid-ossp"; --- +goose StatementEnd \ No newline at end of file diff --git a/migrator/migrations/20240808094749_create_users_table.sql b/migrator/migrations/20240819162009_create_users_table.sql similarity index 51% rename from migrator/migrations/20240808094749_create_users_table.sql rename to migrator/migrations/20240819162009_create_users_table.sql index f4e2b95..e115306 100644 --- a/migrator/migrations/20240808094749_create_users_table.sql +++ b/migrator/migrations/20240819162009_create_users_table.sql @@ -1,16 +1,20 @@ --- Active: 1723171055346@@127.0.0.1@5432@postgres@public -- +goose Up -- +goose StatementBegin +DROP TABLE IF EXISTS users; CREATE TABLE IF NOT EXISTS users( id UUID NOT NULL PRIMARY KEY, name VARCHAR(50) NOT NULL, - email VARCHAR(50) NOT NULL, + email VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(500) NOT NULL, - phone VARCHAR(50) + phone VARCHAR(50) NOT NULL UNIQUE ); +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; +CREATE INDEX email_index ON users (email); +CREATE INDEX phone_index ON users (phone); -- +goose StatementEnd -- +goose Down -- +goose StatementBegin DROP TABLE IF EXISTS users; +DROP EXTENSION IF EXISTS "uuid-ossp"; -- +goose StatementEnd