Skip to content

Commit

Permalink
🐛 bugfix HttpContext is not available.
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Nov 2, 2024
1 parent 907e45d commit f464278
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 40 deletions.
4 changes: 3 additions & 1 deletion src/Server.UI/Components/Fusion/OnlineUsersTracker.razor
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@

public override async ValueTask DisposeAsync()
{
await OnlineUserTracker.Logout();
if (!string.IsNullOrEmpty(currentUserId)){
await OnlineUserTracker.Clear(currentUserId);
}
GC.Collect();
}
}
1 change: 0 additions & 1 deletion src/Server.UI/Services/Fusion/IOnlineUserTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace CleanArchitecture.Blazor.Server.UI.Services.Fusion;
public interface IOnlineUserTracker : IComputeService
{
Task Initial(SessionInfo sessionInfo,CancellationToken cancellationToken = default);
Task Logout(CancellationToken cancellationToken = default);
Task Clear(string userId,CancellationToken cancellationToken = default);
Task Update(string userId,string userName,string displayName,string profilePictureDataUrl, CancellationToken cancellationToken = default);
[ComputeMethod]
Expand Down
39 changes: 1 addition & 38 deletions src/Server.UI/Services/Fusion/OnlineUserTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,7 @@ public virtual async Task Initial(SessionInfo sessionInfo, CancellationToken can
}
}

/// <summary>
/// Logs out the current user.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
public virtual async Task Logout(CancellationToken cancellationToken = default)
{
if (Invalidation.IsActive)
return;
var sessionInfo = await GetSessionInfo().ConfigureAwait(false);
var userSessions = _activeUserSessions.Where(s => s.UserId == sessionInfo.UserId).ToList();
foreach (var session in userSessions)
{
_activeUserSessions = _activeUserSessions.Remove(session);
using var invalidating = Invalidation.Begin();
_ = await GetOnlineUsers(cancellationToken).ConfigureAwait(false);
}
}


/// <summary>
/// Clears all sessions for a specific user.
Expand Down Expand Up @@ -109,25 +93,4 @@ public virtual Task<List<SessionInfo>> GetOnlineUsers(CancellationToken cancella

return Task.FromResult(_activeUserSessions.ToList());
}

/// <summary>
/// Gets the session information for the current user.
/// </summary>
/// <returns>The session information.</returns>
/// <exception cref="InvalidOperationException">Thrown when the HTTP context is not available.</exception>
private Task<SessionInfo> GetSessionInfo()
{
var httpContext = _httpContextAccessor.HttpContext;
if (httpContext == null)
{
throw new InvalidOperationException("HttpContext is not available.");
}
var httpUser = _httpContextAccessor.HttpContext?.User;
var ipAddress = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress?.ToString();
var userId = _httpContextAccessor.HttpContext?.User?.GetUserId();
var userName = _httpContextAccessor.HttpContext?.User?.GetUserName();
var displayName = _httpContextAccessor.HttpContext?.User?.GetDisplayName();
var tenantId = _httpContextAccessor.HttpContext?.User?.GetTenantId();
return Task.FromResult(new SessionInfo(userId, userName, displayName, ipAddress, tenantId, "", UserPresence.Available));
}
}

0 comments on commit f464278

Please sign in to comment.