Skip to content

Commit

Permalink
Added Delete User
Browse files Browse the repository at this point in the history
  • Loading branch information
Ju-Wiluis William Rodriguez Hernandez committed Jun 23, 2024
1 parent 27067de commit 3de4446
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
14 changes: 13 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func ValidateUser(oid, username, password string) (string, error) {

func InsertUser(oid, username, password string) (string, error) {
if err := open(); err != nil {
fmt.Errorf("No se pudo establecer conexion con la base de datos")
println("No se pudo establecer conexion con la base de datos")
return "", err
}
defer closeDB() // Cierra la conexión solo si se abrió correctamente
Expand All @@ -88,3 +88,15 @@ func InsertUser(oid, username, password string) (string, error) {

return id, nil
}

func DeleteUser(oid, userId string) error {
if err := open(); err != nil {
println("No se pudo establecer conexion con la base de datos")
return err
}
defer closeDB() // Cierra la conexión solo si se abrió correctamente

_, err := db.Exec("DELETE FROM credentials WHERE oid = $1 AND id = $2", oid, userId)

return err
}
22 changes: 22 additions & 0 deletions handlers/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package handlers

import (
"encoding/json"
"net/http"

"github.com/Luis97lol/auth-service/database"
"github.com/gorilla/mux"
)

func DeleteHandler(w http.ResponseWriter, r *http.Request) {
// Read token from Authorization header
LogoutHandler(w, r)
vars := mux.Vars(r)
oid := vars["oid"]
userId := vars["userId"]
database.DeleteUser(oid, userId)

// Respond with username
response := map[string]string{"response": "Session closed successfully"}
json.NewEncoder(w).Encode(response)
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func main() {
r.HandleFunc("/validate", handlers.ValidateHandler).Methods("GET")
r.HandleFunc("/renew", handlers.RenewHandler).Methods("GET")
r.HandleFunc("/logout", handlers.LogoutHandler).Methods("GET")
r.HandleFunc("/org/{oid}/user/{userId}", handlers.DeleteHandler).Methods("DELETE")

port := os.Getenv("SERVER_PORT")
if port == "" {
Expand Down

0 comments on commit 3de4446

Please sign in to comment.