Tencent/WeKnora · error

user not found in context

Error message

user not found in context

What it means

Raised in GetCurrentUser when the request context carries no *types.User under types.UserContextKey — the auth middleware did not run or did not inject the user, so the handler bypassed authentication. Context-shape guard.

Source

Thrown at internal/application/service/user.go:1396

// RevokeToken revokes a token
func (s *userService) RevokeToken(ctx context.Context, tokenString string) error {
	tokenRecord, err := s.tokenRepo.GetTokenByValue(ctx, tokenString)
	if err != nil {
		return err
	}

	tokenRecord.IsRevoked = true
	tokenRecord.UpdatedAt = time.Now()

	return s.tokenRepo.UpdateToken(ctx, tokenRecord)
}

// GetCurrentUser gets current user from context
func (s *userService) GetCurrentUser(ctx context.Context) (*types.User, error) {
	user, ok := ctx.Value(types.UserContextKey).(*types.User)
	if !ok {
		return nil, errors.New("user not found in context")
	}

	return user, nil
}

// SearchUsers searches users by username or email
func (s *userService) SearchUsers(ctx context.Context, query string, limit int) ([]*types.User, error) {
	if query == "" {
		return []*types.User{}, nil
	}
	return s.userRepo.SearchUsers(ctx, query, limit)
}

type oidcDiscoveryDocument struct {
	AuthorizationEndpoint string `json:"authorization_endpoint"`
	TokenEndpoint         string `json:"token_endpoint"`
	UserInfoEndpoint      string `json:"userinfo_endpoint"`
	JwksURI               string `json:"jwks_uri"`

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Ensure the route is protected by the auth middleware before calling GetCurrentUser
  2. Return 401 instead of treating it as a normal business error
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at internal/application/service/user.go:1396 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/4c8298cfe9fab8f5. Report an issue: GitHub.