MCPcopy Create free account
hub / github.com/supabase/auth / adminUserDelete

Method adminUserDelete

internal/api/admin.go:512–576  ·  view source on GitHub ↗

adminUserDelete deletes a user

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

510
511// adminUserDelete deletes a user
512func (a *API) adminUserDelete(w http.ResponseWriter, r *http.Request) error {
513 ctx := r.Context()
514 user := getUser(ctx)
515 config := a.config
516 adminUser := getAdminUser(ctx)
517 db := a.db.WithContext(ctx)
518
519 // ShouldSoftDelete defaults to false
520 params := &adminUserDeleteParams{}
521 if body, _ := utilities.GetBodyBytes(r); len(body) != 0 {
522 // we only want to parse the body if it's not empty
523 // retrieveRequestParams will handle any errors with stream
524 if err := retrieveRequestParams(r, params); err != nil {
525 return err
526 }
527 }
528
529 err := db.Transaction(func(tx *storage.Connection) error {
530 if terr := models.NewAuditLogEntry(config.AuditLog, r, tx, adminUser, models.UserDeletedAction, "", map[string]interface{}{
531 "user_id": user.ID,
532 "user_email": user.Email,
533 "user_phone": user.Phone,
534 }); terr != nil {
535 return apierrors.NewInternalServerError("Error recording audit log entry").WithInternalError(terr)
536 }
537
538 if params.ShouldSoftDelete {
539 if user.DeletedAt != nil {
540 // user has been soft deleted already
541 return nil
542 }
543 if terr := user.SoftDeleteUser(tx); terr != nil {
544 return apierrors.NewInternalServerError("Error soft deleting user").WithInternalError(terr)
545 }
546
547 if terr := user.SoftDeleteUserIdentities(tx); terr != nil {
548 return apierrors.NewInternalServerError("Error soft deleting user identities").WithInternalError(terr)
549 }
550
551 // hard delete all associated factors
552 if terr := models.DeleteFactorsByUserId(tx, user.ID); terr != nil {
553 return apierrors.NewInternalServerError("Error deleting user's factors").WithInternalError(terr)
554 }
555 // hard delete all associated WebAuthn credentials
556 if terr := models.DeleteWebAuthnCredentialsByUserID(tx, user.ID); terr != nil {
557 return apierrors.NewInternalServerError("Error deleting user's WebAuthn credentials").WithInternalError(terr)
558 }
559 // hard delete all associated sessions
560 if terr := models.Logout(tx, user.ID); terr != nil {
561 return apierrors.NewInternalServerError("Error deleting user's sessions").WithInternalError(terr)
562 }
563 } else {
564 if terr := tx.Destroy(user); terr != nil {
565 return apierrors.NewInternalServerError("Database error deleting user").WithInternalError(terr)
566 }
567 }
568
569 return nil

Callers

nothing calls this directly

Calls 15

GetBodyBytesFunction · 0.92
NewAuditLogEntryFunction · 0.92
NewInternalServerErrorFunction · 0.92
DeleteFactorsByUserIdFunction · 0.92
LogoutFunction · 0.92
getUserFunction · 0.85
getAdminUserFunction · 0.85
retrieveRequestParamsFunction · 0.85
sendJSONFunction · 0.85
WithContextMethod · 0.80
TransactionMethod · 0.80

Tested by

no test coverage detected