@cbnsndwch/zero-sources

isAuthenticated() function

Home > @cbnsndwch/zrocket-contracts > isAuthenticated

isAuthenticated() function

Type guard to check if the query context contains required authentication information.

Signature:

declare function isAuthenticated(
    ctx: QueryContext | undefined
): ctx is QueryContext;

Parameters

Parameter

Type

Description

ctx

QueryContext | undefined

The query context to check, which may be undefined for anonymous requests

Returns:

ctx is QueryContext

true if the context is defined and contains a valid sub (user ID), false otherwise

Remarks

This type guard can be used to narrow the type of the context from QueryContext | undefined to QueryContext, enabling TypeScript to provide proper type checking for authenticated-only code paths.

Example

function myQuery(builder, ctx) {
    if (!isAuthenticated(ctx)) {
        // Handle anonymous user case
        return builder.publicChannels.all();
    }

    // ctx is now typed as QueryContext (not undefined)
    return builder.rooms.where('ownerId', '=', ctx.sub).all();
}

How was this page?