Skip to content

Commit

Permalink
fix(auth): field to register by social account on MySQL database (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Jun 6, 2024
1 parent f8f509c commit 7567afd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 9 additions & 4 deletions internal/auth/social_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (

func RegisterSocialUser(dao *dao.Dao, u SocialUser) (entity.AuthIdentity, error) {
if u.Name == "" {
return entity.AuthIdentity{}, fmt.Errorf("name is required")
return entity.AuthIdentity{}, fmt.Errorf("cannot fetch user name from social identity provider")
}
if u.Email == "" {
return entity.AuthIdentity{}, fmt.Errorf("email is required")
return entity.AuthIdentity{}, fmt.Errorf("cannot fetch user email from social identity provider")
}
if !utils.ValidateEmail(u.Email) {
return entity.AuthIdentity{}, fmt.Errorf("email is invalid")
return entity.AuthIdentity{}, fmt.Errorf("email is invalid which fetched from social identity provider")
}

// Create user if not exists
Expand All @@ -26,6 +26,11 @@ func RegisterSocialUser(dao *dao.Dao, u SocialUser) (entity.AuthIdentity, error)
return entity.AuthIdentity{}, err
}

var expiresAt *time.Time
if !u.ExpiresAt.IsZero() {
expiresAt = &u.ExpiresAt
}

// Store user auth identity in db
now := time.Now()
authIdentity := entity.AuthIdentity{
Expand All @@ -34,7 +39,7 @@ func RegisterSocialUser(dao *dao.Dao, u SocialUser) (entity.AuthIdentity, error)
UserID: user.ID,
Token: u.AccessToken,
ConfirmedAt: &now,
ExpiresAt: &u.ExpiresAt,
ExpiresAt: expiresAt,
}
if err := dao.CreateAuthIdentity(&authIdentity); err != nil {
return entity.AuthIdentity{}, err
Expand Down
12 changes: 11 additions & 1 deletion internal/auth/social_user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package auth

import "github.com/markbates/goth"
import (
"strings"

"github.com/markbates/goth"
)

type SocialUser struct {
goth.User
Expand All @@ -27,6 +31,12 @@ func GetSocialUser(u goth.User) SocialUser {
u.Email = u.UserID + "@" + u.Provider + ".com"
}

// Name patch
if u.Name == "" && u.Email != "" {
// try extract name from email
u.Name = strings.Split(u.Email, "@")[0]
}

return SocialUser{
User: u,
RemoteUID: u.UserID,
Expand Down

0 comments on commit 7567afd

Please sign in to comment.