to set a cookie does not work

import type { BetterAuthPlugin } from 'better-auth';
import { createAuthEndpoint } from 'better-auth/api';
import { z } from 'zod';

const setSchool = createAuthEndpoint(
'/schools/set-school',
{
method: 'POST',
body: z.object({ schoolId: z.string(), name: z.string() }),
},
async (ctx) => {
const { schoolId: id, name } = ctx.body;

await ctx.setSignedCookie(
'school',
JSON.stringify({ id, name }),
ctx.context.secret
);

return ctx.json({ success: true });
}
);

export const schools = () => {
return {
id: 'schools',
endpoints: {
setSchool,
},
} satisfies BetterAuthPlugin;
};
import type { BetterAuthPlugin } from 'better-auth';
import { createAuthEndpoint } from 'better-auth/api';
import { z } from 'zod';

const setSchool = createAuthEndpoint(
'/schools/set-school',
{
method: 'POST',
body: z.object({ schoolId: z.string(), name: z.string() }),
},
async (ctx) => {
const { schoolId: id, name } = ctx.body;

await ctx.setSignedCookie(
'school',
JSON.stringify({ id, name }),
ctx.context.secret
);

return ctx.json({ success: true });
}
);

export const schools = () => {
return {
id: 'schools',
endpoints: {
setSchool,
},
} satisfies BetterAuthPlugin;
};
usage:
'use server';

import { auth } from '@/lib/auth';

export async function action() {
const result = await auth.api.setSchool({
method: 'POST',
body: {
name: 'Default School',
schoolId: 'default-school-id',
},
});

console.log(result);
}
'use server';

import { auth } from '@/lib/auth';

export async function action() {
const result = await auth.api.setSchool({
method: 'POST',
body: {
name: 'Default School',
schoolId: 'default-school-id',
},
});

console.log(result);
}
is this the correct way?
1 Reply
Ping
Ping5mo ago
@BannedNull Can i see your auth config?

Did you find this page helpful?