diff --git a/builder/store/database/user.go b/builder/store/database/user.go index 0e7595ee..4cc9d55c 100644 --- a/builder/store/database/user.go +++ b/builder/store/database/user.go @@ -197,10 +197,13 @@ func (s *UserStore) FindByGitAccessToken(ctx context.Context, token string) (*Us return &user, nil } -func (s *UserStore) FindByUUID(ctx context.Context, uuid string) (user User, err error) { - user.UUID = uuid - err = s.db.Operator.Core.NewSelect().Model(&user).Where("uuid = ?", uuid).Scan(ctx) - return +func (s *UserStore) FindByUUID(ctx context.Context, uuid string) (*User, error) { + var user User + err := s.db.Operator.Core.NewSelect().Model(&user).Where("uuid = ?", uuid).Scan(ctx) + if err != nil { + return nil, err + } + return &user, nil } func (s *UserStore) GetActiveUserCount(ctx context.Context) (int, error) { diff --git a/user/component/user.go b/user/component/user.go index 182a9a19..352873c8 100644 --- a/user/component/user.go +++ b/user/component/user.go @@ -8,6 +8,7 @@ import ( "log/slog" "os" "sync" + "time" "github.com/bwmarrin/snowflake" "github.com/casdoor/casdoor-go-sdk/casdoorsdk" @@ -415,16 +416,26 @@ func (c *UserComponent) Signin(ctx context.Context, code, state string) (*types. return nil, "", fmt.Errorf("failed to check user existance by name in db,error:%w", err) } + var dbu *database.User if !exists { - _, err = c.createFromCasdoorUser(ctx, cu) + dbu, err = c.createFromCasdoorUser(ctx, cu) if err != nil { return nil, "", fmt.Errorf("failed to create user,error:%w", err) } - } - // get user from db for username, as casdoor may have different username - dbu, err := c.us.FindByUUID(ctx, cu.Id) - if err != nil { - return nil, "", fmt.Errorf("failed to find user by uuid in db, uuid:%s, error:%w", cu.Id, err) + } else { + // get user from db for username, as casdoor may have different username + dbu, err = c.us.FindByUUID(ctx, cu.Id) + if err != nil { + return nil, "", fmt.Errorf("failed to find user by uuid in db, uuid:%s, error:%w", cu.Id, err) + } + // update user login time asynchronously + go func() { + dbu.LastLoginAt = time.Now().Format("2006-01-02 15:04:05") + err := c.us.Update(ctx, dbu) + if err != nil { + slog.Error("failed to update user login time", "error", err, "username", dbu.Username) + } + }() } hubToken, signed, err := c.jwtc.GenerateToken(ctx, types.CreateJWTReq{ UUID: dbu.UUID,