Skip to content

Commit c1429b2

Browse files
committed
feat: demo smallest possible hooks change
1 parent 88bb2c0 commit c1429b2

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

internal/api/anonymous.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ func (a *API) SignupAnonymously(w http.ResponseWriter, r *http.Request) error {
4141
if terr != nil {
4242
return terr
4343
}
44+
if terr := a.triggerAfterUserCreated(); terr != nil {
45+
return terr
46+
}
4447
token, terr = a.issueRefreshToken(r, tx, newUser, models.Anonymous, grantParams)
4548
if terr != nil {
4649
return terr

internal/api/external.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ func (a *API) createAccountFromExternalIdentity(tx *storage.Connection, r *http.
334334
return nil, terr
335335
}
336336
user.Identities = append(user.Identities, *identity)
337+
338+
if terr := a.triggerAfterUserCreated(); terr != nil {
339+
return nil, terr
340+
}
337341
case models.AccountExists:
338342
user = decision.User
339343
identity = decision.Identities[0]

internal/api/invite.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func (a *API) Invite(w http.ResponseWriter, r *http.Request) error {
7171
return err
7272
}
7373
user.Identities = []models.Identity{*identity}
74+
75+
if err := a.triggerAfterUserCreated(); err != nil {
76+
return err
77+
}
7478
}
7579

7680
if terr := models.NewAuditLogEntry(r, tx, adminUser, models.UserInvitedAction, "", map[string]interface{}{

internal/api/signup.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/supabase/auth/internal/api/apierrors"
1212
"github.com/supabase/auth/internal/api/provider"
1313
"github.com/supabase/auth/internal/api/sms_provider"
14+
"github.com/supabase/auth/internal/hooks/v0hooks"
1415
"github.com/supabase/auth/internal/metering"
1516
"github.com/supabase/auth/internal/models"
1617
"github.com/supabase/auth/internal/storage"
@@ -366,6 +367,10 @@ func sanitizeUser(u *models.User, params *SignupParams) (*models.User, error) {
366367
func (a *API) signupNewUser(conn *storage.Connection, user *models.User) (*models.User, error) {
367368
config := a.config
368369

370+
if err := a.triggerBeforeUserCreated(); err != nil {
371+
return user, err
372+
}
373+
369374
err := conn.Transaction(func(tx *storage.Connection) error {
370375
var terr error
371376
if terr = tx.Create(user); terr != nil {
@@ -389,3 +394,25 @@ func (a *API) signupNewUser(conn *storage.Connection, user *models.User) (*model
389394

390395
return user, nil
391396
}
397+
398+
func (a *API) triggerAfterUserCreated( /* todo: params */ ) error {
399+
input := v0hooks.AfterUserCreatedOutput{}
400+
output := v0hooks.AfterUserCreatedOutput{}
401+
err := a.hooksMgr.InvokeHook(nil, nil, &input, &output)
402+
if err != nil {
403+
return err
404+
}
405+
406+
return nil
407+
}
408+
409+
func (a *API) triggerBeforeUserCreated( /* todo: params */ ) error {
410+
input := v0hooks.BeforeUserCreatedOutput{}
411+
output := v0hooks.BeforeUserCreatedOutput{}
412+
err := a.hooksMgr.InvokeHook(nil, nil, &input, &output)
413+
if err != nil {
414+
return err
415+
}
416+
417+
return nil
418+
}

internal/hooks/v0hooks/v0hooks.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,38 @@ type AccessTokenClaims struct {
107107
IsAnonymous bool `json:"is_anonymous"`
108108
}
109109

110+
type BeforeUserCreatedInput struct {
111+
User *models.User `json:"user"`
112+
}
113+
114+
type BeforeUserCreatedOutput struct {
115+
Something string `json:"something"`
116+
}
117+
118+
func (mf *BeforeUserCreatedOutput) IsError() bool {
119+
return false
120+
}
121+
122+
func (mf *BeforeUserCreatedOutput) Error() string {
123+
return ""
124+
}
125+
126+
type AfterUserCreatedInput struct {
127+
User *models.User `json:"user"`
128+
}
129+
130+
type AfterUserCreatedOutput struct {
131+
Something string `json:"something"`
132+
}
133+
134+
func (mf *AfterUserCreatedOutput) IsError() bool {
135+
return false
136+
}
137+
138+
func (mf *AfterUserCreatedOutput) Error() string {
139+
return ""
140+
}
141+
110142
type MFAVerificationAttemptInput struct {
111143
UserID uuid.UUID `json:"user_id"`
112144
FactorID uuid.UUID `json:"factor_id"`

0 commit comments

Comments
 (0)