mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-25 00:22:43 -05:00
fix(magic-link): return additional fields in /magic-link/verify endpoint
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user