{"record":{"id":"2d3604a3af57b339","repo":"fullstackhero/dotnet-starter-kit","slug":"no-current-user-createchannelcommandhandler","errorCode":null,"errorMessage":"no current user","messagePattern":"no current user","errorType":"exception","errorClass":"UnauthorizedException","httpStatus":401,"severity":"error","filePath":"src/Modules/Chat/Modules.Chat/Features/v1/Channels/CreateChannel/CreateChannelCommandHandler.cs","lineNumber":21,"sourceCode":"using FSH.Modules.Chat.Contracts.v1.Commands;\nusing FSH.Modules.Chat.Data;\nusing FSH.Modules.Chat.Domain;\nusing Mediator;\n\nnamespace FSH.Modules.Chat.Features.v1.Channels.CreateChannel;\n\npublic sealed class CreateChannelCommandHandler(\n    ChatDbContext db,\n    ICurrentUser currentUser)\n    : ICommandHandler<CreateChannelCommand, Guid>\n{\n    public async ValueTask<Guid> Handle(CreateChannelCommand cmd, CancellationToken cancellationToken)\n    {\n        ArgumentNullException.ThrowIfNull(cmd);\n        var userId = currentUser.GetUserId().ToString();\n        if (userId == Guid.Empty.ToString())\n        {\n            throw new UnauthorizedException(\"no current user\");\n        }\n\n        var channel = ChatChannel.CreateChannel(cmd.Name, cmd.Description, cmd.IsPrivate, userId);\n        db.Channels.Add(channel);\n        await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);\n        return channel.Id;\n    }\n}\n","sourceCodeStart":3,"sourceCodeEnd":30,"githubUrl":"https://github.com/fullstackhero/dotnet-starter-kit/blob/3f2959e683e9f83f13e55e1678c9119f63c7e8e5/src/Modules/Chat/Modules.Chat/Features/v1/Channels/CreateChannel/CreateChannelCommandHandler.cs#L3-L30","documentation":"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.","triggerScenarios":"Calling CreateChannel without a valid JWT, with an anonymous or expired identity, or unit-testing the handler with an ICurrentUser stub returning Guid.Empty.","commonSituations":"Missing Authorization header; token expired mid-session; test fixtures that never set up ICurrentUser.","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"],"exampleFix":"// before\nvar userId = currentUser.GetUserId().ToString();\nif (userId == Guid.Empty.ToString()) throw new UnauthorizedException(\"no current user\");\n// after (robust check)\nvar userId = currentUser.GetUserId();\nif (userId == Guid.Empty) throw new UnauthorizedException(\"no current user\");","handlingStrategy":"validation","validationCode":"var userId = currentUser.GetUserId();\nif (userId == Guid.Empty) throw new UnauthorizedException(\"no current user\");","typeGuard":"bool isAuthenticated = currentUser.GetUserId() != Guid.Empty;","tryCatchPattern":"try { await handler.Handle(cmd, ct); }\ncatch (UnauthorizedException) { /* return 401 / re-login */ }","preventionTips":["Authenticate before channel creation calls","Handle 401 with token refresh","Configure ICurrentUser in test fixtures"],"tags":["auth","unauthorized","chat","current-user"],"backgroundTag":"authentication-required","analyzedSha":"3f2959e683e9f83f13e55e1678c9119f63c7e8e5","analyzedAt":"2026-09-15T22:20:53.684Z","contentChangedAt":"2026-09-15T22:20:53.684Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}