[GH-ISSUE #1063] feature-request: Allow returning of headers object when calling relevant .api methods #8575

Closed
opened 2026-04-13 03:41:41 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @lkuoch on GitHub (Dec 29, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1063

Is this suited for github?

  • Yes, this is suited for github

I'm integrating better-auth in it's own micro-service and am facing issues with the .api methods not setting cookies correctly.

I think this is due to the fact that all the .api methods are itself a http call underneath which is proving a bit of trouble as I want to put better-auth in it's own service that the client doesn't interact with directly.

If possible, I would like a config option to return a special cookie header value back which I can then use in my own middleware to set the appropriate cookies for the client.

Describe the solution you'd like

This feature should be entirely optional and live as a config under advanced eg:

export const auth = betterAuth({
    advanced: {
        cookies: {
            // False by default
            return_cookies: true        
        }
    }
})

For api methods that modify cookies eg:

await auth.api.signInEmail({
    body: {
        email: "",
        password: ""
    }
})

the return object would include a special value such as $cookies eg:

// This should return the actual cookie value
const cookies = await setSessionCookie(
	ctx,
	{
		session,
		user: user.user,
	},
	ctx.body.rememberMe === false,
);
return ctx.json({
    // This should be based on the config
  	...(config.advanced.cookies.return_cookies ? {'$cookies': cookies} : {}),
    
	user: {
		id: user.user.id,
		email: user.user.email,
		name: user.user.name,
		image: user.user.image,
		emailVerified: user.user.emailVerified,
		createdAt: user.user.createdAt,
		updatedAt: user.user.updatedAt,
	},
	token: session.token,
	redirect: !!ctx.body.callbackURL,
	url: ctx.body.callbackURL,
});

Describe alternatives you've considered

Many attempts at hacking better-auth itself

Additional context

No response

Originally created by @lkuoch on GitHub (Dec 29, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1063 ### Is this suited for github? - [X] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. I'm integrating better-auth in it's own micro-service and am facing issues with the `.api` methods not setting cookies correctly. I think this is due to the fact that all the `.api` methods are itself a http call underneath which is proving a bit of trouble as I want to put better-auth in it's own service that the client doesn't interact with directly. If possible, I would like a config option to return a special cookie header value back which I can then use in my own middleware to set the appropriate cookies for the client. ### Describe the solution you'd like This feature should be entirely optional and live as a config under `advanced` eg: ```ts export const auth = betterAuth({ advanced: { cookies: { // False by default return_cookies: true } } }) ``` For api methods that modify cookies eg: ```ts await auth.api.signInEmail({ body: { email: "", password: "" } }) ``` the return object would include a special value such as `$cookies` eg: ```ts // This should return the actual cookie value const cookies = await setSessionCookie( ctx, { session, user: user.user, }, ctx.body.rememberMe === false, ); return ctx.json({ // This should be based on the config ...(config.advanced.cookies.return_cookies ? {'$cookies': cookies} : {}), user: { id: user.user.id, email: user.user.email, name: user.user.name, image: user.user.image, emailVerified: user.user.emailVerified, createdAt: user.user.createdAt, updatedAt: user.user.updatedAt, }, token: session.token, redirect: !!ctx.body.callbackURL, url: ctx.body.callbackURL, }); ``` ### Describe alternatives you've considered Many attempts at hacking better-auth itself ### Additional context _No response_
GiteaMirror added the locked label 2026-04-13 03:41:41 -05:00
Author
Owner

@lkuoch commented on GitHub (Dec 30, 2024):

@Bekacru is this feasible in your opinion?

<!-- gh-comment-id:2565964040 --> @lkuoch commented on GitHub (Dec 30, 2024): @Bekacru is this feasible in your opinion?
Author
Owner

@Scooter1337 commented on GitHub (Dec 30, 2024):

In the meantime you should be able to use 'asResponse: true', no?

https://www.better-auth.com/docs/concepts/api#getting-the-response-object

<!-- gh-comment-id:2566016523 --> @Scooter1337 commented on GitHub (Dec 30, 2024): In the meantime you should be able to use 'asResponse: true', no? https://www.better-auth.com/docs/concepts/api#getting-the-response-object
Author
Owner

@Bekacru commented on GitHub (Dec 31, 2024):

yeah use asResponse:true to get the response as Response object instead

<!-- gh-comment-id:2566153800 --> @Bekacru commented on GitHub (Dec 31, 2024): yeah use `asResponse:true` to get the response as Response object instead
Author
Owner

@lkuoch commented on GitHub (Jan 1, 2025):

Awesome!

<!-- gh-comment-id:2567163750 --> @lkuoch commented on GitHub (Jan 1, 2025): Awesome!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8575