forked from supabase/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanonymous.go
More file actions
62 lines (53 loc) · 1.58 KB
/
anonymous.go
File metadata and controls
62 lines (53 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package api
import (
"net/http"
"github.com/supabase/auth/internal/api/apierrors"
"github.com/supabase/auth/internal/metering"
"github.com/supabase/auth/internal/models"
"github.com/supabase/auth/internal/storage"
)
func (a *API) SignupAnonymously(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
config := a.config
db := a.db.WithContext(ctx)
aud := a.requestAud(ctx, r)
if config.DisableSignup {
return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeSignupDisabled, "Signups not allowed for this instance")
}
params := &SignupParams{}
if err := retrieveRequestParams(r, params); err != nil {
return err
}
params.Aud = aud
params.Provider = "anonymous"
newUser, err := params.ToUserModel(false /* <- isSSOUser */)
if err != nil {
return err
}
if err := a.triggerBeforeUserCreated(r, db, newUser); err != nil {
return err
}
var grantParams models.GrantParams
grantParams.FillGrantParams(r)
var token *AccessTokenResponse
err = db.Transaction(func(tx *storage.Connection) error {
var terr error
newUser, terr = a.signupNewUser(tx, newUser)
if terr != nil {
return terr
}
token, terr = a.issueRefreshToken(r, tx, newUser, models.Anonymous, grantParams)
if terr != nil {
return terr
}
return nil
})
if err != nil {
return apierrors.NewInternalServerError("Database error creating anonymous user").WithInternalError(err)
}
if err := a.triggerAfterUserCreated(r, db, newUser); err != nil {
return err
}
metering.RecordLogin(metering.LoginTypeAnonymous, newUser.ID, nil)
return sendTokenJSON(w, http.StatusOK, token)
}