@@ -28,6 +28,7 @@ import (
2828 "github.com/getsentry/sentry-go"
2929 "github.com/google/uuid"
3030 "github.com/pkg/errors"
31+ "sync"
3132)
3233
3334// An Authenticator manages user authentication for the Playground API.
@@ -37,6 +38,7 @@ import (
3738type Authenticator struct {
3839 store storage.Store
3940 sessionName string
41+ lock sync.Mutex
4042}
4143
4244// NewAuthenticator returns a new authenticator instance.
@@ -57,39 +59,32 @@ const userIDKey = "userID"
5759// GetOrCreateUser gets an existing user from the current session or creates a
5860// new user and session if a session does not already exist.
5961func (a * Authenticator ) GetOrCreateUser (ctx context.Context ) (* model.User , error ) {
60- session := sessions .Get (ctx , a .sessionName )
61-
62- var user * model.User
63- var err error
62+ a .lock .Lock ()
63+ defer a .lock .Unlock ()
6464
65- userLoaded := false
66-
67- if ! session .IsNew {
68- // Try to load existing user
69- if session .Values [userIDKey ] != nil {
70- user , err = a .getCurrentUser (session .Values [userIDKey ].(string ))
71- if err != nil {
72- sentry .CaptureException (errors .New (fmt .Sprintf (
73- "Failed to load user id %s from session\n " , session .Values [userIDKey ].(string ))))
74- } else {
75- userLoaded = true
76- }
77- }
78- }
65+ session := sessions .Get (ctx , a .sessionName )
7966
80- if ! userLoaded {
81- // Create new user
82- user , err = a .createNewUser ()
67+ if session . Values [ userIDKey ] == nil {
68+ // Create new user since UserID for cookie has not been created yet
69+ user , err : = a .createNewUser ()
8370 if err != nil {
8471 return nil , errors .Wrap (err , "failed to create new user" )
8572 }
8673
8774 session .Values [userIDKey ] = user .ID .String ()
75+
76+ err = sessions .Save (ctx , session )
77+ if err != nil {
78+ fmt .Println ("Failed to save session!" )
79+ return nil , errors .Wrap (err , "failed to save userID to session" )
80+ }
8881 }
8982
90- err = sessions . Save ( ctx , session )
83+ user , err := a . getCurrentUser ( session . Values [ userIDKey ].( string ) )
9184 if err != nil {
92- return nil , errors .Wrap (err , "failed to update session" )
85+ sentry .CaptureException (errors .New (fmt .Sprintf (
86+ "Failed to load user id %s from session\n " , session .Values [userIDKey ].(string ))))
87+ return nil , errors .New ("failed to load user id from session" )
9388 }
9489
9590 return user , nil
@@ -101,6 +96,9 @@ func (a *Authenticator) GetOrCreateUser(ctx context.Context) (*model.User, error
10196// This function checks for access using both the new and legacy authentication schemes. If
10297// a user has legacy access, their authentication is then migrated to use the new scheme.
10398func (a * Authenticator ) CheckProjectAccess (ctx context.Context , proj * model.Project ) error {
99+ a .lock .Lock ()
100+ defer a .lock .Unlock ()
101+
104102 session := sessions .Get (ctx , a .sessionName )
105103
106104 if session .Values [userIDKey ] == nil {
0 commit comments