diff --git a/openapi/oauth/providers/user/user_basic.go b/openapi/oauth/providers/user/user_basic.go index c97fde6b..29d1d5be 100644 --- a/openapi/oauth/providers/user/user_basic.go +++ b/openapi/oauth/providers/user/user_basic.go @@ -468,24 +468,27 @@ func (u *DefaultUser) DeleteUser(ctx context.Context, userID string) error { // UpdateUserLastLogin updates the user's last login timestamp and context func (u *DefaultUser) UpdateUserLastLogin(ctx context.Context, userID string, loginCtx *types.LoginContext) error { + // Validate loginCtx is required + if loginCtx == nil { + return fmt.Errorf("loginCtx is required") + } + updateData := maps.MapStrAny{ "last_login_at": time.Now(), } - // Add login context fields if provided - if loginCtx != nil { - if loginCtx.IP != "" { - updateData["last_login_ip"] = loginCtx.IP - } - if loginCtx.UserAgent != "" { - updateData["last_login_user_agent"] = loginCtx.UserAgent - } - if loginCtx.Device != "" { - updateData["last_login_device"] = loginCtx.Device - } - if loginCtx.Platform != "" { - updateData["last_login_platform"] = loginCtx.Platform - } + // Add login context fields + if loginCtx.IP != "" { + updateData["last_login_ip"] = loginCtx.IP + } + if loginCtx.UserAgent != "" { + updateData["last_login_user_agent"] = loginCtx.UserAgent + } + if loginCtx.Device != "" { + updateData["last_login_device"] = loginCtx.Device + } + if loginCtx.Platform != "" { + updateData["last_login_platform"] = loginCtx.Platform } return u.UpdateUser(ctx, userID, updateData) diff --git a/openapi/oauth/providers/user/user_basic_test.go b/openapi/oauth/providers/user/user_basic_test.go index 4402686e..366fb0a6 100644 --- a/openapi/oauth/providers/user/user_basic_test.go +++ b/openapi/oauth/providers/user/user_basic_test.go @@ -221,9 +221,10 @@ func TestUserBasicOperations(t *testing.T) { assert.Equal(t, "desktop", user["last_login_device"]) assert.Equal(t, "web", user["last_login_platform"]) - // Test with nil loginCtx (should only update timestamp) + // Test with nil loginCtx (should return error) err = testProvider.UpdateUserLastLogin(ctx, testUserID, nil) - assert.NoError(t, err) + assert.Error(t, err) + assert.Contains(t, err.Error(), "loginCtx is required") // Test with partial loginCtx (only IP) partialCtx := &types.LoginContext{