Create authorization by role
This commit is contained in:
@@ -9,6 +9,7 @@ type Credentials struct {
|
|||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
Phone string `json:"phone"`
|
Phone string `json:"phone"`
|
||||||
|
Role string `json:"role"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Claims struct {
|
type Claims struct {
|
||||||
@@ -16,4 +17,4 @@ type Claims struct {
|
|||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Phone string `json:"phone"`
|
Phone string `json:"phone"`
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
|
|||||||
},
|
},
|
||||||
Email: user.Email,
|
Email: user.Email,
|
||||||
Phone: user.Phone,
|
Phone: user.Phone,
|
||||||
|
Role: user.Role,
|
||||||
}
|
}
|
||||||
|
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
|||||||
@@ -34,8 +34,10 @@ func Register(w http.ResponseWriter, r *http.Request) {
|
|||||||
Email: Credentials.Email,
|
Email: Credentials.Email,
|
||||||
Password: hashedPassword,
|
Password: hashedPassword,
|
||||||
Phone: Credentials.Phone,
|
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)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func AuthAdminMiddleware(next http.Handler) http.Handler {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if claims.Role != "Admin" {
|
if claims.Role != "admin" {
|
||||||
w.WriteHeader(http.StatusNonAuthoritativeInfo)
|
w.WriteHeader(http.StatusNonAuthoritativeInfo)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user