Create authorization by role

This commit is contained in:
valitovgaziz
2024-08-25 06:07:48 +05:00
parent 0e50f3826f
commit 1f967a28f7
4 changed files with 7 additions and 3 deletions
+2 -1
View File
@@ -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"`
}
}
+1
View File
@@ -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)
+3 -1
View File
@@ -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
}
+1 -1
View File
@@ -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
}