Documentation
    Preparing search index...

    Module Fastify JWT

    @byu-oit-sdk/fastify-jwt

    Requirements:

    • Node.js 20+
    • npm v9+
    • Fastify v5
    • @byu-oit-sdk/fastify-jwt now requires Node.js 20 or newer.
    • @byu-oit-sdk/fastify-jwt now targets Fastify 5.
    npm install @byu-oit-sdk/fastify-jwt
    

    This fastify middleware allows for verification and decoding of JWTs.

    Along with the options for the CreateJwt() function (see docs for jwt package), the following options can be passed in when registering the middleware with fastify.

    Option Type Default Description
    prefix string - Used to specify what route the middleware is registered with.
    transformer JwtPayloadTransformer<Payload, Transformer> - The function that you will use for manipulating the JWT you are authenticating.
    getJwt function - A function for getting the JWT from the request. By default, the JWT is pulled from the auth header.
    validate boolean - Boolean used to signify if we want to validate the jwt if true, or just decode it if false.
    TypeScript Coding Example
    import Fastify from 'fastify'
    import { FastifyJwtProvider } from '@byu-oit-sdk/fastify-jwt'
    import { Type } from '@sinclair/typebox'

    // set up fastify server
    const app = Fastify({
    logger: true
    })

    // declare transformer
    const transformer = (payload: { 'http://byu.edu/claims/client_preferred_first_name': string }): { preferred_first_name: string } => ({ preferred_first_name: payload['http://byu.edu/claims/client_preferred_first_name'] })

    // register the middleware with Fastify
    await app.register(FastifyJwtProvider, { schema: Type.Object({ }, { additionalProperties: true }), key: '', validate: false, transformer })

    // declare endpoint
    app.get('/', async (request, reply) => {
    return request.caller
    })

    // send a request
    const response = await app.inject({
    method: 'GET',
    url: '/',
    headers: {
    Authorization: /* A valid token */
    }
    })

    console.log(response)

    Interfaces

    JwtAuthenticatorOptions
    JwtPayload
    VerifyJwtPluginOptions

    Type Aliases

    GetJwtFn

    Variables

    FastifyJwtProvider
    getJwtFromAssertionHeader
    getJwtFromAuthHeader

    Functions

    createJwtAuthenticationHandler
    JwtAuthenticator