Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/services/notify/events/email-verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
emailTemplateTtlMs,
emailVerificationManifestUrl,
urlEmailVerification,
VERIFY_LIFESPAN_MS,
} from "../../../config/constants";
import type User from "../../../data/entity/user.entity";
import { fetchJsonFromUrl } from "../../../data/utils";
Expand Down Expand Up @@ -114,11 +115,10 @@ export async function sendEmailVerification(
throw new Error("User email is required for verification");
}

const token = jwt.sign({
id: user.id,
email: user.email,
type: "verify" as TokenType,
});
const token = jwt.sign(
{ id: user.id, email: user.email, type: "verify" as TokenType },
{ expiresIn: `${VERIFY_LIFESPAN_MS}` },
);
const url = `${urlEmailVerification}/${token}`;

logger.debug(`sendEmailVerification: ${user.email}, url: ${url}`);
Expand Down
16 changes: 14 additions & 2 deletions src/test/services/notify/email-verification.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Lang } from "need4deed-sdk";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { urlEmailVerification } from "../../../config/constants";
import { urlEmailVerification, VERIFY_LIFESPAN_MS } from "../../../config/constants";
import { fetchJsonFromUrl } from "../../../data/utils";
import {
resetVerificationTemplateCache,
Expand All @@ -12,7 +12,8 @@ vi.mock("../../../data/utils", () => ({
}));

const send = vi.fn();
const deps = { email: { send }, jwt: { sign: () => "tok" } } as any;
const sign = vi.fn(() => "tok");
const deps = { email: { send }, jwt: { sign } } as any;
const user = (over: any = {}) => ({ id: 1, email: "u@x.de", ...over });
const expectedUrl = `${urlEmailVerification}/tok`;

Expand Down Expand Up @@ -90,4 +91,15 @@ describe("sendEmailVerification", () => {
sendEmailVerification(deps, user({ email: undefined })),
).rejects.toThrow("User email is required");
});

it("signs the token with VERIFY_LIFESPAN_MS as expiresIn", async () => {
vi.mocked(fetchJsonFromUrl).mockResolvedValue(manifest);

await sendEmailVerification(deps, user());

expect(sign).toHaveBeenCalledWith(
expect.objectContaining({ type: "verify" }),
expect.objectContaining({ expiresIn: `${VERIFY_LIFESPAN_MS}` }),
);
});
});