Skip to content

Commit

Permalink
Improved Delete Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ju-Wiluis William Rodriguez Hernandez committed Jun 24, 2024
1 parent 859e6e1 commit 000fcfb
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions handlers/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,27 @@ func DeleteHandler(w http.ResponseWriter, r *http.Request) {
oid := vars["oid"]
userId := vars["userId"]

token := extractToken(r.Header.Get("Authorization"))
admintoken := extractToken(r.Header.Get("Authorization"))
if admintoken == "" {
writeJsonError(w, "Token not provided", http.StatusBadRequest)
return
}

// Validate token against Redis
_, err := redis.ValidateToken(token)
_, err := redis.ValidateToken(admintoken)
if err != nil {
json.NewEncoder(w).Encode(map[string]string{"error": "Token not valid"})
return
}

token, err := redis.ValidateUser(userId)
if err == nil {
redis.DeleteToken(token, userId)
}

database.DeleteUser(oid, userId)

// Respond with username
response := map[string]string{"response": "Session closed successfully"}
response := map[string]string{"response": "User deleted successfully"}
json.NewEncoder(w).Encode(response)
}

0 comments on commit 000fcfb

Please sign in to comment.