Skip to content

Commit

Permalink
☢️ bug: on first user creation, user stored in state is null version …
Browse files Browse the repository at this point in the history
…(e.g. uuid: 0000-0000-0000-0000)
  • Loading branch information
garrettladley committed Jun 18, 2024
1 parent d3e4997 commit b235f52
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 8 additions & 6 deletions backend/entities/auth/base/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ func FindOrCreateUser(ctx context.Context, db *gorm.DB, user soth.User) (*models
var sacUser models.User
if err := db.WithContext(ctx).Where("email = ?", user.Email).First(&sacUser).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if err := createUser(ctx, db, user.Into()); err != nil {
user, err := createUser(ctx, db, *user.Into())
if err != nil {
return nil, err
}
return user, nil
} else {
return nil, err
}
Expand All @@ -24,24 +26,24 @@ func FindOrCreateUser(ctx context.Context, db *gorm.DB, user soth.User) (*models
return &sacUser, nil
}

func createUser(ctx context.Context, db *gorm.DB, user *models.User) error {
func createUser(ctx context.Context, db *gorm.DB, user models.User) (*models.User, error) {
tx := db.WithContext(ctx).Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()

if err := tx.Create(user).Error; err != nil {
if err := tx.Create(&user).Error; err != nil {
tx.Rollback()
return err
return nil, err
}

welcomeTask := models.WelcomeTask{Name: user.Name, Email: user.Email}
if err := tx.Create(&welcomeTask).Error; err != nil {
tx.Rollback()
return err
return nil, err
}

return tx.Commit().Error
return &user, tx.Commit().Error
}
4 changes: 4 additions & 0 deletions backend/integrations/oauth/soth/sothic/sothic.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,20 @@ func updateSessionValue(session *session.Session, key, value string) error {
if _, err := gz.Write([]byte(value)); err != nil {
return err
}

if err := gz.Flush(); err != nil {
return err
}

if err := gz.Close(); err != nil {
return err
}

encrypted, err := encrypter(b.String())
if err != nil {
return err
}

session.Set(key, encrypted)
return nil
}

0 comments on commit b235f52

Please sign in to comment.