Skip to main content

Fastify

info

Available in >2.0.0

This adapter provides utility functions to convert between Fastify Request and Reply objects and the OAuthRequest/OAuthResponse objects used by this package.

Functions

requestFromFastify(req: FastifyRequest): OAuthRequest
handleFastifyReply(fastifyReply: FastifyReply, oauthResponse: OAuthResponse): void
handleFastifyError(reply: FastifyReply, e: unknown | OAuthException): void

Example

import { requestFromFastify, handleFastifyReply, handleFastifyError } from "@jmondi/oauth2-server/fastify";
import fastify from 'fastify'

const app = fastify()

// ...

app.post('/oauth2/token', async (request: fastify.Request, reply: fastify.Reply) => {
const authorizationServer = request.server.authorizationServer;

try {
const oauthResponse = await authorizationServer
.respondToAccessTokenRequest(requestFromFastify(request));

handleFastifyReply(reply, oauthResponse);
} catch (e) {
handleFastifyError(reply, e);
}
});