Skip to content

Commit

Permalink
fix(data): resolve provider merge bug
Browse files Browse the repository at this point in the history
where user signs in with one or more providers with the same email
address, the previous provider ID would be emptied instead of appended
to.
  • Loading branch information
scottmckendry committed Aug 24, 2024
1 parent e3a00b3 commit b9f38f0
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (s *Storage) CreateOrUpdateUser(gothUser goth.User) error {
}
}

appendProviderID(gothUser.Provider, gothUser.UserID, user)
err = updateUser(s.db, user)
if err != nil {
return fmt.Errorf("Error updating user: %v", err)
Expand All @@ -55,6 +56,15 @@ func (s *Storage) CreateOrUpdateUser(gothUser goth.User) error {
return nil
}

func appendProviderID(provider string, userId string, user *User) {
switch provider {
case "discord":
user.DiscordID = userId
case "github":
user.GithubID = userId
}
}

func getUser(db *sql.DB, user *User) error {
row := db.QueryRow("SELECT * FROM users WHERE email = ?", user.Email)
err := row.Scan(&user.ID, &user.Name, &user.Email, &user.DiscordID, &user.GithubID)
Expand Down Expand Up @@ -110,13 +120,7 @@ func buildUserFromGothUser(gothUser goth.User) *User {
Email: gothUser.Email,
}

switch gothUser.Provider {
case "discord":
user.DiscordID = gothUser.UserID
case "github":
user.GithubID = gothUser.UserID
}

appendProviderID(gothUser.Provider, gothUser.UserID, user)
return user
}

Expand Down

0 comments on commit b9f38f0

Please sign in to comment.