function

CSRF.generate

function generate(
secret?: string,
): string;

Generate a CSRF token.

@param secret

The secret to use for the token. If not provided, Bun generates a random default secret in memory and uses it.

@param options

The options for the token.

@returns

The generated token.

Referenced types

interface CSRFGenerateOptions

  • algorithm?: CSRFAlgorithm

    The algorithm to use for the token.

  • encoding?: 'base64' | 'base64url' | 'hex'

    The encoding of the token.

  • expiresIn?: number

    The number of milliseconds until the token expires. 0 means the token never expires.

  • sessionId?: string

    Binds the token to the requesting principal (session ID, user ID, or equivalent). A token generated with a sessionId only verifies when the same sessionId is supplied to verify(). Without it, any token issued under the same secret validates for every user.