8000 fix: invites should send another email when user exists by cstockton · Pull Request #2058 · supabase/auth · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: invites should send another email when user exists #2058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions internal/api/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,38 +37,50 @@ func (a *API) Invite(w http.ResponseWriter, r *http.Request) error {
if err != nil && !models.IsNotFoundError(err) {
return apierrors.NewInternalServerError("Database error finding user").WithInternalError(err)
}
if user != nil && user.IsConfirmed() {
return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeEmailExists, DuplicateEmailMsg)
}

signupParams := SignupParams{
Email: params.Email,
Data: params.Data,
Aud: aud,
Provider: "email",
}
isCreate := user == nil
isConfirmed := user != nil && user.IsConfirmed()

user, err = signupParams.ToUserModel(false /* <- isSSOUser */)
if err != nil {
return err
}
if err := a.triggerBeforeUserCreated(r, db, user); err != nil {
return err
}
if isCreate {
signupParams := SignupParams{
Email: params.Email,
Data: params.Data,
Aud: aud,
Provider: "email",
}

err = db.Transaction(func(tx *storage.Connection) error {
user, err = a.signupNewUser(tx, user)
// because params above sets no password, this method
// is not computationally hard so it can be used within
// a database transaction
user, err = signupParams.ToUserModel(false /* <- isSSOUser */)
if err != nil {
return err
}
identity, err := a.createNewIdentity(tx, user, "email", structs.Map(provider.Claims{
Subject: user.ID.String(),
Email: user.GetEmail(),
}))
if err != nil {

if err := a.triggerBeforeUserCreated(r, db, user); err != nil {
return err
}
user.Identities = []models.Identity{*identity}
}

err = db.Transaction(func(tx *storage.Connection) error {
if !isCreate {
if isConfirmed {
return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeEmailExists, DuplicateEmailMsg)
}
} else {
user, err = a.signupNewUser(tx, user)
if err != nil {
return err
}
identity, err := a.createNewIdentity(tx, user, "email", structs.Map(provider.Claims{
Subject: user.ID.String(),
Email: user.GetEmail(),
}))
if err != nil {
return err
}
user.Identities = []models.Identity{*identity}
}

if terr := models.NewAuditLogEntry(r, tx, adminUser, models.UserInvitedAction, "", map[string]interface{}{
"user_id": user.ID,
Expand Down
52 changes: 52 additions & 0 deletions internal/api/invite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/gofrs/uuid"
jwt "github.com/golang-jwt/jwt/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -101,6 +102,57 @@ func (ts *InviteTestSuite) TestInvite() {
assert.Equal(ts.T(), http.StatusOK, w.Code)
}

func (ts *InviteTestSuite) TestInviteExists() {
// To allow us to send signup and invite request in succession
ts.Config.SMTP.MaxFrequency = 200

email := uuid.Must(uuid.NewV4()).String() + "@example.com"

{
// Request body
var buffer bytes.Buffer
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]interface{}{
"email": email,
"data": map[string]interface{}{
"a": 1,
},
}))

// Setup request
req := httptest.NewRequest(http.MethodPost, "http://localhost/invite", &buffer)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", ts.token))

// Setup response recorder
w := httptest.NewRecorder()

ts.API.handler.ServeHTTP(w, req)
assert.Equal(ts.T(), http.StatusOK, w.Code)
}

{
// Request body
var buffer bytes.Buffer
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]interface{}{
"email": email,
"data": map[string]interface{}{
"a": 1,
},
}))

// Setup request
req := httptest.NewRequest(http.MethodPost, "http://localhost/invite", &buffer)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", ts.token))

// Setup response recorder
w := httptest.NewRecorder()

ts.API.handler.ServeHTTP(w, req)
assert.Equal(ts.T(), http.StatusOK, w.Code)
}
}

func (ts *InviteTestSuite) TestInviteAfterSignupShouldNotReturnSensitiveFields() {
// To allow us to send signup and invite request in succession
ts.Config.SMTP.MaxFrequency = 5
Expand Down
Loading
0