Create authorization by role
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user