forked from mirrors/pronouns.cc
42 lines
931 B
Go
42 lines
931 B
Go
package user
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"codeberg.org/u1f320/pronouns.cc/backend/server"
|
|
"emperror.dev/errors"
|
|
"github.com/go-chi/render"
|
|
)
|
|
|
|
func (s *Server) deleteUser(w http.ResponseWriter, r *http.Request) error {
|
|
ctx := r.Context()
|
|
claims, _ := server.ClaimsFromContext(ctx)
|
|
|
|
if claims.APIToken || !claims.TokenWrite {
|
|
return server.APIError{Code: server.ErrMissingPermissions}
|
|
}
|
|
|
|
tx, err := s.DB.Begin(ctx)
|
|
if err != nil {
|
|
return errors.Wrap(err, "creating transaction")
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
|
|
err = s.DB.DeleteUser(ctx, tx, claims.UserID, true, "")
|
|
if err != nil {
|
|
return errors.Wrap(err, "setting user as deleted")
|
|
}
|
|
|
|
err = s.DB.InvalidateAllTokens(ctx, tx, claims.UserID)
|
|
if err != nil {
|
|
return errors.Wrap(err, "invalidating tokens")
|
|
}
|
|
|
|
err = tx.Commit(ctx)
|
|
if err != nil {
|
|
return errors.Wrap(err, "committing transaction")
|
|
}
|
|
|
|
render.JSON(w, r, map[string]any{"deleted": true})
|
|
return nil
|
|
}
|