Skip to content

Commit

Permalink
Adjust user
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Sep 15, 2024
1 parent ccfc540 commit 4076490
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
25 changes: 18 additions & 7 deletions cmd/web_server/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/oj-lab/platform/cmd/web_server/middleware"
user_model "github.com/oj-lab/platform/models/user"
gorm_agent "github.com/oj-lab/platform/modules/agent/gorm"
Expand Down Expand Up @@ -41,26 +42,36 @@ func githubCallback(ginCtx *gin.Context) {
return
}

user, _ := user_service.GetUser(ginCtx, githubUser.Login)
if user == nil {
users, total, err := user_service.GetUserList(ginCtx, user_model.GetUserOptions{
GithubLogin: &githubUser.Login,
})
if err != nil {
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to get user list: %v", err))
return
}
var user *user_model.User
if total <= 0 {
uuid := uuid.New()
user, err = user_service.CreateUser(ginCtx, user_model.User{
Account: githubUser.Login,
Name: githubUser.Name,
Email: &githubUser.Email,
AvatarURL: githubUser.AvatarURL,
Account: uuid.String(),
Name: githubUser.Name,
Email: &githubUser.Email,
AvatarURL: githubUser.AvatarURL,
GithubLogin: &githubUser.Login,
})
if err != nil {
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to create user: %v", err))
return
}
} else {
user = &users[0]
}

ls, err := user_service.StartLoginSession(ginCtx, user.Account)
if err != nil {
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to start login session: %v", err))
return
}

middleware.SetLoginSessionKeyCookie(ginCtx, ls.Key)

ginCtx.JSON(200, nil)
Expand Down
1 change: 1 addition & 0 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type User struct {
HashedPassword string `json:"-" gorm:"not null"`
Email *string `json:"email,omitempty" gorm:"unique"`
AvatarURL string `json:"avatarUrl"`
GithubLogin *string `json:"githubLogin" gorm:"unique"`
}

var PublicUserSelection = append([]string{"account", "name", "avatar_url"}, models.MetaFieldsSelection...)
4 changes: 4 additions & 0 deletions models/user/user_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type GetUserOptions struct {
DomainRole *casbin_agent.DomainRole
Offset *int
Limit *int
GithubLogin *string
}

func buildGetUserTXByOptions(tx *gorm.DB, options GetUserOptions, isCount bool) *gorm.DB {
Expand All @@ -126,6 +127,9 @@ func buildGetUserTXByOptions(tx *gorm.DB, options GetUserOptions, isCount bool)
if options.EmailQuery != "" {
tx = tx.Where("email LIKE ?", options.EmailQuery)
}
if options.GithubLogin != nil {
tx = tx.Where("github_login = ?", *options.GithubLogin)
}

if !isCount {
if options.Offset != nil {
Expand Down

0 comments on commit 4076490

Please sign in to comment.