feat(plugin): Third-party login to synchronize user avatars

This commit is contained in:
LinkinStars 2023-03-02 10:24:32 +08:00
parent 1065946c43
commit 4c38c73017
5 changed files with 26 additions and 2 deletions

View File

@ -80,6 +80,7 @@ func (cc *ConnectorController) ConnectorRedirect(connector plugin.Connector) (fn
ExternalID: userInfo.ExternalID,
Name: userInfo.Name,
Email: userInfo.Email,
Avatar: userInfo.Avatar,
MetaInfo: userInfo.MetaInfo,
}
resp, err := cc.userExternalService.ExternalLogin(ctx, u)

View File

@ -43,6 +43,8 @@ type ExternalLoginUserInfoCache struct {
Name string
// optional. If email exist will bind the existing user
Email string
// optional. The avatar URL provided by the third-party login platform
Avatar string
// optional. The original user information provided by the third-party login platform
MetaInfo string
}

View File

@ -103,6 +103,12 @@ func (r *GetUserToSetShowResp) GetFromUserEntity(userInfo *entity.User) {
r.Avatar = avatarInfo
}
const (
AvatarTypeDefault = "default"
AvatarTypeGravatar = "gravatar"
AvatarTypeCustom = "custom"
)
func FormatAvatarInfo(avatarJson string) string {
if avatarJson == "" {
return ""
@ -113,9 +119,9 @@ func FormatAvatarInfo(avatarJson string) string {
return ""
}
switch avatarInfo.Type {
case "gravatar":
case AvatarTypeGravatar:
return avatarInfo.Gravatar
case "custom":
case AvatarTypeCustom:
return avatarInfo.Custom
default:
return ""

View File

@ -2,6 +2,7 @@ package user_external_login
import (
"context"
"encoding/json"
"fmt"
"time"
@ -127,10 +128,22 @@ func (us *UserExternalLoginService) registerNewUser(ctx context.Context,
userInfo = &entity.User{}
userInfo.EMail = externalUserInfo.Email
userInfo.DisplayName = externalUserInfo.Name
userInfo.Username, err = us.userCommonService.MakeUsername(ctx, externalUserInfo.Name)
if err != nil {
log.Error(err)
userInfo.Username = random.Username()
}
if len(externalUserInfo.Avatar) > 0 {
avatarInfo := &schema.AvatarInfo{
Type: schema.AvatarTypeCustom,
Custom: externalUserInfo.Avatar,
}
avatar, _ := json.Marshal(avatarInfo)
userInfo.Avatar = string(avatar)
}
userInfo.MailStatus = entity.EmailStatusToBeVerified
userInfo.Status = entity.UserStatusAvailable
userInfo.LastLoginDate = time.Now()

View File

@ -33,6 +33,8 @@ type ExternalLoginUserInfo struct {
Name string
// optional. If email exist will bind the existing user
Email string
// optional. The avatar URL provided by the third-party login platform
Avatar string
// optional. The original user information provided by the third-party login platform
MetaInfo string
}