BA
Better Authโ€ข3mo ago
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 })
}
}
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
}
}
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 !
GitHub
sandbox/js_ts/admin/src/modules/better-auth at base ยท danyalutsevic...
Only I understand what is going on here. You don't need to - danyalutsevich/sandbox
No description
12 Replies
bekacru
bekacruโ€ข3mo ago
this means the body is getting parsed by nest js before it reaches better auth hanlder. not familiar with nest but basically you need to prevent the body from being pre-parsed
cercio
cercioOPโ€ข3mo ago
Ok i will investigate, thanks you ! Worked like a charm, thanks you so much
const app = await NestFactory.create(AppModule, {
bodyParser: false
})
const app = await NestFactory.create(AppModule, {
bodyParser: false
})
Just have to disable bodyParser, globally or on a specific root with middleware. If you need documentation or source code on nestJS implementation, i can provide you some stuff as much as i can ๐Ÿ™‚
bekacru
bekacruโ€ข3mo ago
yeah please do. would apperciate that. we don't have a documentation for nest js yet
cercio
cercioOPโ€ข3mo ago
Okay i will start to document my implementation ๐Ÿ‘
Unknown User
Unknown Userโ€ข3mo ago
Message Not Public
Sign In & Join Server To View
cercio
cercioOPโ€ข3mo ago
Hi ! Working on it, i keep you in touch soon For now, do you want my repo ?
Unknown User
Unknown Userโ€ข3mo ago
Message Not Public
Sign In & Join Server To View
cercio
cercioOPโ€ข3mo ago
Im returning from vacation, sending you this asap ๐Ÿ‘ here it is : https://github.com/Gaetandrt/nestjs-better-auth-exemple @Tivot @David (zyzer) ๐Ÿš€ I have setup better-auth and its working but i have not experience it a lot for now. There maybe is a better way to do it. Don't forget to disable bodyParser. I have done it globally as a work around but if you find a way to disable it only for the better auth root, its better.
const app = await NestFactory.create(AppModule, {
bodyParser: false,
});
const app = await NestFactory.create(AppModule, {
bodyParser: false,
});
Unknown User
Unknown Userโ€ข3mo ago
Message Not Public
Sign In & Join Server To View
cercio
cercioOPโ€ข3mo ago
Nice workaround for the bodyParser, for the decorator and session i didn't investigate more than the stup for now so i can't help you here
Unknown User
Unknown Userโ€ข3mo ago
Message Not Public
Sign In & Join Server To View
cercio
cercioOPโ€ข3mo ago
Keep me in touch if you find workaround / tips for the nestJS implementation as i'm writting the doc

Did you find this page helpful?