How to implement a custom response interceptor with Nest Better Auth library?

I'm using the Nest Better Auth library and need help implementing a custom interceptor to transform the library's default response format to match my application's standardized response structure. Current Issue The Better Auth library returns responses in this format:
{
"code": "USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL",
"message": "User already exists. Use another email."
}
{
"code": "USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL",
"message": "User already exists. Use another email."
}
or
{
"code": "INVALID_EMAIL_OR_PASSWORD",
"message": "Invalid email or password"
}
{
"code": "INVALID_EMAIL_OR_PASSWORD",
"message": "Invalid email or password"
}
I need to transform these responses to match my API's standard format:
{
"status": "error",
"message": "User already exists. Use another email.",
"data": null,
"statusCode": 400
}
{
"status": "error",
"message": "User already exists. Use another email.",
"data": null,
"statusCode": 400
}
What I've Tried - Interceptors - Exception filters - Middleware Questions How can I intercept Better Auth responses before they're sent to the client? Is there a built-in way to customize the response format in Better Auth, or do I need to wrap all endpoints? Can you provide an example of a working interceptor that transforms Better Auth responses? Environment: NestJS version: 11.0.1 Better Auth version: ^1.3.16 @thallesp/nestjs-better-auth: ^2.0.0
GitHub
GitHub - ThallesP/nestjs-better-auth: Easily add authentication to ...
Easily add authentication to your project. Contribute to ThallesP/nestjs-better-auth development by creating an account on GitHub.
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?