Better AuthBA
Better Auth14mo ago
18 replies
cercio

TypeError: Response body object should not be disturbed or locked

I got this error and have no clue how to resolve it. I use Better auth on my nestJS server, see my implementation :

auth.service.ts
import { Injectable } from '@nestjs/common'
import { PrismaClient } from '@prisma/client'
import { betterAuth } from 'better-auth'
import { prismaAdapter } from 'better-auth/adapters/prisma'

@Injectable()
export class AuthService {
  public betterAuth: any

  constructor() {
    const auth = betterAuth({
      // plugins: [expo()],
      database: prismaAdapter(PrismaClient, {
        provider: 'postgresql'
      }),
      trustedOrigins: ['myapp://'],
      advanced: {
        disableCSRFCheck: true
      },
      emailAndPassword: {
        enabled: true
      }
    })

    this.betterAuth = auth
    console.log({ auth })
  }
}


auth.controller.ts
import { All, Controller, Logger, Req, Res } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'

import { toNodeHandler } from 'better-auth/node'
import { AuthService } from './auth.service'

@Controller('api/auth')
@ApiTags('api/auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}
  private readonly logger = new Logger(AuthController.name)

  @All('*')
  async handleAuth(@Req() req: Request, @Res() res: Response) {
    this.logger.log('handleAuth')
    if (!this.authService.betterAuth) {
      throw new Error('BetterAuth not initialized')
    }

    console.log(req.url)
    const authHandler = toNodeHandler(this.authService.betterAuth)

    const result = await authHandler(req as any, res as any)
    console.log('result', result)

    return result
  }
}


I have used this implementation from another better auth user : https://github.com/danyalutsevich/sandbox/tree/base/js_ts/admin/src/modules/better-auth

Using better-auth expo client on a react-native app.

Thanks in advance !
CleanShot_2024-12-28_at_16.32.012x.png
GitHub
Only I understand what is going on here. You don't need to - danyalutsevich/sandbox
sandbox/js_ts/admin/src/modules/better-auth at base · danyalutsevic...
Was this page helpful?