fix(magic-link): return additional fields in /magic-link/verify endpoint

This commit is contained in:
Alex Yang
2026-01-09 10:03:34 -08:00
parent f869c4874b
commit 8fba8fff2b
2 changed files with 74 additions and 9 deletions

View File

@@ -8,6 +8,7 @@ import * as z from "zod";
import { originCheck } from "../../api";
import { setSessionCookie } from "../../cookies";
import { generateRandomString } from "../../crypto";
import { parseSessionOutput, parseUserOutput } from "../../db";
import { defaultKeyHasher } from "./utils";
declare module "@better-auth/core" {
@@ -402,15 +403,8 @@ export const magicLink = (options: MagicLinkOptions) => {
if (!ctx.query.callbackURL) {
return ctx.json({
token: session.token,
user: {
id: user.id,
email: user.email,
emailVerified: user.emailVerified,
name: user.name,
image: user.image,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
},
user: parseUserOutput(ctx.context.options, user),
session: parseSessionOutput(ctx.context.options, session),
});
}
if (isNewUser) {

View File

@@ -272,6 +272,77 @@ describe("magic link", async () => {
expect(customGenerateToken).toHaveBeenCalled();
expect(verificationEmail.token).toBe("custom_token");
});
it("should return additional fields", async () => {
const { customFetchImpl, sessionSetter, auth } = await getTestInstance({
user: {
additionalFields: {
foo: {
type: "string",
required: false,
},
},
},
plugins: [
magicLink({
async sendMagicLink(data) {
verificationEmail = data;
},
}),
],
});
const client = createAuthClient({
plugins: [magicLinkClient()],
fetchOptions: {
customFetchImpl,
},
baseURL: "http://localhost:3000/api/auth",
});
const email = "test-email@test.com";
await client.signIn.magicLink({
email,
});
const headers = new Headers();
const response = await client.magicLink.verify({
query: {
token: new URL(verificationEmail.url).searchParams.get("token") || "",
},
fetchOptions: {
onSuccess: sessionSetter(headers),
},
});
expect(response.data?.user).toBeDefined();
// @ts-expect-error
expect(response.data?.user.foo).toBeNull();
await auth.api.updateUser({
body: {
foo: "bar",
},
headers,
});
await client.signIn.magicLink({
email,
});
{
const response = await client.magicLink.verify({
query: {
token: new URL(verificationEmail.url).searchParams.get("token")!,
},
fetchOptions: {
onSuccess: sessionSetter(headers),
},
});
// @ts-expect-error
expect(response.data?.user.foo).toBe("bar");
}
});
});
describe("magic link verify", async () => {