From 1f967a28f79f46ebecd3216fc26cde744f27b0e3 Mon Sep 17 00:00:00 2001 From: valitovgaziz Date: Sun, 25 Aug 2024 06:07:48 +0500 Subject: [PATCH] Create authorization by role --- api/src/models/authDataStructs.go | 3 ++- api/src/rt/auth/Login.go | 1 + api/src/rt/auth/Registr.go | 4 +++- api/src/rt/auth/authAdminMiddlware.go | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/api/src/models/authDataStructs.go b/api/src/models/authDataStructs.go index fc4c885..2bdd814 100644 --- a/api/src/models/authDataStructs.go +++ b/api/src/models/authDataStructs.go @@ -9,6 +9,7 @@ type Credentials struct { Email string `json:"email"` Password string `json:"password"` Phone string `json:"phone"` + Role string `json:"role"` } type Claims struct { @@ -16,4 +17,4 @@ type Claims struct { Email string `json:"email"` Phone string `json:"phone"` Role string `json:"role"` -} \ No newline at end of file +} diff --git a/api/src/rt/auth/Login.go b/api/src/rt/auth/Login.go index b7987ad..a01f131 100644 --- a/api/src/rt/auth/Login.go +++ b/api/src/rt/auth/Login.go @@ -37,6 +37,7 @@ func Login(w http.ResponseWriter, r *http.Request) { }, Email: user.Email, Phone: user.Phone, + Role: user.Role, } token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) diff --git a/api/src/rt/auth/Registr.go b/api/src/rt/auth/Registr.go index e7ce904..babbd58 100644 --- a/api/src/rt/auth/Registr.go +++ b/api/src/rt/auth/Registr.go @@ -34,8 +34,10 @@ func Register(w http.ResponseWriter, r *http.Request) { Email: Credentials.Email, Password: hashedPassword, Phone: Credentials.Phone, + Role: Credentials.Role, } - if result := psql.PSQL_GORM_DB.Create(&user); result.Error != nil { + result := psql.PSQL_GORM_DB.Create(&user) + if result.Error != nil { w.WriteHeader(http.StatusInternalServerError) return } diff --git a/api/src/rt/auth/authAdminMiddlware.go b/api/src/rt/auth/authAdminMiddlware.go index c761e50..e4a049c 100644 --- a/api/src/rt/auth/authAdminMiddlware.go +++ b/api/src/rt/auth/authAdminMiddlware.go @@ -40,7 +40,7 @@ func AuthAdminMiddleware(next http.Handler) http.Handler { return } - if claims.Role != "Admin" { + if claims.Role != "admin" { w.WriteHeader(http.StatusNonAuthoritativeInfo) return }