fullstackhero/dotnet-starter-kit · error · UnauthorizedException
no current user
Error message
no current user
What it means
CreateChannelCommandHandler throws UnauthorizedException when the current user id resolves to Guid.Empty (compared via its string form), meaning the request had no authenticated user. A channel cannot be created without an owner/creator identity.
Solutions
- Authenticate the request with a valid bearer token
- Handle 401/expiry in the client by refreshing the session
- In tests, configure ICurrentUser.GetUserId() to return a real Guid
Example fix
// before
var userId = currentUser.GetUserId().ToString();
if (userId == Guid.Empty.ToString()) throw new UnauthorizedException("no current user");
// after (robust check)
var userId = currentUser.GetUserId();
if (userId == Guid.Empty) throw new UnauthorizedException("no current user"); Defensive patterns
Strategy: validation
Validate before calling
var userId = currentUser.GetUserId();
if (userId == Guid.Empty) throw new UnauthorizedException("no current user"); Type guard
bool isAuthenticated = currentUser.GetUserId() != Guid.Empty;
Try / catch
try { await handler.Handle(cmd, ct); }
catch (UnauthorizedException) { /* return 401 / re-login */ } Prevention
- Authenticate before channel creation calls
- Handle 401 with token refresh
- Configure ICurrentUser in test fixtures
When it happens
Trigger: Calling CreateChannel without a valid JWT, with an anonymous or expired identity, or unit-testing the handler with an ICurrentUser stub returning Guid.Empty.
Common situations: Missing Authorization header; token expired mid-session; test fixtures that never set up ICurrentUser.
Related errors
AI-assisted analysis of fullstackhero/dotnet-starter-kit@3f2959e683 (2026-09-15).
Data as JSON: /api/errors/2d3604a3af57b339.
Report an issue: GitHub.
Appendix: source
Thrown at src/Modules/Chat/Modules.Chat/Features/v1/Channels/CreateChannel/CreateChannelCommandHandler.cs:21
using FSH.Modules.Chat.Contracts.v1.Commands;
using FSH.Modules.Chat.Data;
using FSH.Modules.Chat.Domain;
using Mediator;
namespace FSH.Modules.Chat.Features.v1.Channels.CreateChannel;
public sealed class CreateChannelCommandHandler(
ChatDbContext db,
ICurrentUser currentUser)
: ICommandHandler<CreateChannelCommand, Guid>
{
public async ValueTask<Guid> Handle(CreateChannelCommand cmd, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(cmd);
var userId = currentUser.GetUserId().ToString();
if (userId == Guid.Empty.ToString())
{
throw new UnauthorizedException("no current user");
}
var channel = ChatChannel.CreateChannel(cmd.Name, cmd.Description, cmd.IsPrivate, userId);
db.Channels.Add(channel);
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
return channel.Id;
}
}
View on GitHub (pinned to 3f2959e683)