Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/user/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export { default as hasUserPermission } from "./lib/hasUserPermission";
export { default as ProfileValidationClaim } from "./supertokens/utils/profileValidationClaim";
export { default as createUserContext } from "./supertokens/utils/createUserContext";
export { default as userSchema } from "./graphql/schema";
export { errorHandler as supertokensErrorHandler } from "./supertokens/errorHandler";

export * from "./migrations/queries";

Expand Down
17 changes: 17 additions & 0 deletions packages/user/src/supertokens/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { errorHandler as supertokensErrorHandler } from "supertokens-node/framework/fastify";

export const errorHandler = (
error: Error,
request: FastifyRequest,
reply: FastifyReply,
) => {
return (
supertokensErrorHandler() as unknown as (
this: FastifyInstance,
error: Error,
request: FastifyRequest,
reply: FastifyReply,
) => Promise<void>
).call(request.server, error, request, reply);
};
21 changes: 6 additions & 15 deletions packages/user/src/supertokens/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import cors from "@fastify/cors";
import formDataPlugin from "@fastify/formbody";
import FastifyPlugin from "fastify-plugin";
import supertokens from "supertokens-node";
import {
errorHandler,
plugin as supertokensPlugin,
} from "supertokens-node/framework/fastify";
import { plugin as supertokensPlugin } from "supertokens-node/framework/fastify";
import { verifySession } from "supertokens-node/recipe/session/framework/fastify";

import { errorHandler } from "./errorHandler";
import init from "./init";

import type { FastifyError, FastifyInstance } from "fastify";
import type { FastifyInstance } from "fastify";

const plugin = async (fastify: FastifyInstance) => {
const { config, log } = fastify;
Expand All @@ -19,16 +17,9 @@ const plugin = async (fastify: FastifyInstance) => {

init(fastify);

// Explicitly cast the errorHandler to the correct type
// [RL 2025-04-01] This should be fixed when supertokens-node is updated
fastify.setErrorHandler(
errorHandler() as unknown as (
this: FastifyInstance,
error: FastifyError,
request: import("fastify").FastifyRequest,
reply: import("fastify").FastifyReply,
) => void,
);
if (config.user.supertokens.setErrorHandler !== false) {
fastify.setErrorHandler(errorHandler);
}

await fastify.register(cors, {
origin: config.appOrigin,
Expand Down
1 change: 1 addition & 0 deletions packages/user/src/supertokens/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface SupertokensConfig {
resetPasswordPath?: string;
emailVerificationPath?: string;
sendUserAlreadyExistsWarning?: boolean;
setErrorHandler?: boolean;
}

export type { SupertokensConfig, SupertokensRecipes };