fix: signup (#25334)

* Handle Stripe logic in `paymentCallback`

* Remove endpoint

* Do not send email verification email if premium username

* Remove logic from verify-view

* Callback send verification email

* Add `create` method to `VerificationToken` repository

* Create `VerificationTokenService`

* Early return if payment failed

* Refactor token generation

* Add tests

* Type fixes

* Type fixes
This commit is contained in:
Joe Au-Yeung authored and GitHub committed 2025-11-21 19:51:19 +00:00
1 parent 06348982a3
commit e5ebf7feb4
12 files changed
+714 -104

No files matched your search

@@ -0,0 +1,164 @@
import { describe, test, expect, vi, beforeEach } from "vitest";
import type { VerificationToken } from "@calcom/prisma/client";
import { VerificationTokenRepository } from "../verificationToken";
vi.mock("@calcom/prisma", () => ({
default: {
verificationToken: {
create: vi.fn(),
findFirst: vi.fn(),
update: vi.fn(),
},
},
}));
const prismaMock = vi.hoisted(() => ({
verificationToken: {
create: vi.fn(),
findFirst: vi.fn(),
update: vi.fn(),
},
}));
vi.mock("@calcom/prisma", () => ({
default: prismaMock,
}));
describe("VerificationTokenRepository", () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe("create", () => {
test("should create a verification token", async () => {
const identifier = "[email protected]";
const token = "hashed-token-value";
const expires = new Date(Date.now() + 86400 * 1000);
const mockVerificationToken: VerificationToken = {
identifier,
token,
expires,
};
prismaMock.verificationToken.create.mockResolvedValue(mockVerificationToken);
const result = await VerificationTokenRepository.create({
identifier,
token,
expires,
});
expect(prismaMock.verificationToken.create).toHaveBeenCalledWith({
data: {
identifier,
token,
expires,
},
});
expect(result).toEqual(mockVerificationToken);
});
test("should create a verification token with correct data types", async () => {
const identifier = "[email protected]";
const token = "sha256-hashed-token";
const expires = new Date("2025-12-31T23:59:59.000Z");
const mockVerificationToken: VerificationToken = {
identifier,
token,
expires,
};
prismaMock.verificationToken.create.mockResolvedValue(mockVerificationToken);
await VerificationTokenRepository.create({
identifier,
token,
expires,
});
expect(prismaMock.verificationToken.create).toHaveBeenCalledWith({
data: {
identifier,
token,
expires,
},
});
});
});
describe("updateTeamInviteTokenExpirationDate", () => {
test("should update team invite token expiration date", async () => {
const email = "[email protected]";
const teamId = 123;
const expiresInDays = 7;
const existingToken: VerificationToken = {
identifier: email,
token: "existing-token",
expires: new Date(),
};
prismaMock.verificationToken.findFirst.mockResolvedValue(existingToken);
prismaMock.verificationToken.update.mockResolvedValue({
...existingToken,
expires: new Date(Date.now() + expiresInDays * 24 * 60 * 60 * 1000),
});
const result = await VerificationTokenRepository.updateTeamInviteTokenExpirationDate({
email,
teamId,
expiresInDays,
});
expect(prismaMock.verificationToken.findFirst).toHaveBeenCalledWith({
where: {
identifier: email,
teamId,
},
});
expect(prismaMock.verificationToken.update).toHaveBeenCalledWith({
where: {
identifier: email,
teamId,
token: existingToken.token,
},
data: { expires: expect.any(Date) },
});
expect(result.token).toBe(existingToken.token);
expect(result.expires).toBeInstanceOf(Date);
});
test("should calculate correct expiration date", async () => {
const email = "[email protected]";
const teamId = 456;
const expiresInDays = 3;
const existingToken: VerificationToken = {
identifier: email,
token: "token-123",
expires: new Date(),
};
prismaMock.verificationToken.findFirst.mockResolvedValue(existingToken);
const expectedExpires = new Date(Date.now() + expiresInDays * 24 * 60 * 60 * 1000);
prismaMock.verificationToken.update.mockImplementation(({ data }) => {
expect(data.expires.getTime()).toBeCloseTo(expectedExpires.getTime(), -2);
return Promise.resolve({ ...existingToken, expires: data.expires });
});
await VerificationTokenRepository.updateTeamInviteTokenExpirationDate({
email,
teamId,
expiresInDays,
});
});
});
});
@@ -30,4 +30,22 @@ export class VerificationTokenRepository {
return { ...token, expires };
}
static async create({
identifier,
token,
expires,
}: {
identifier: string;
token: string;
expires: Date;
}) {
return prisma.verificationToken.create({
data: {
identifier,
token,
expires,
},
});
}
}
@@ -0,0 +1,16 @@
import { randomBytes, createHash } from "crypto";
import { VerificationTokenRepository } from "../repository/verificationToken";
export class VerificationTokenService {
static async create({ identifier, expires }: { identifier: string; expires: Date }) {
const token = randomBytes(32).toString("hex");
const hashedToken = createHash("sha256")
.update(`${token}${process.env.NEXTAUTH_SECRET}`)
.digest("hex");
await VerificationTokenRepository.create({ identifier, token: hashedToken, expires });
return token;
}
}
@@ -0,0 +1,134 @@
import { describe, test, expect, vi, beforeEach, afterEach } from "vitest";
import { VerificationTokenRepository } from "@calcom/lib/server/repository/verificationToken";
import { VerificationTokenService } from "../VerificationTokenService";
vi.mock("@calcom/lib/server/repository/verificationToken", () => ({
VerificationTokenRepository: {
create: vi.fn(),
},
}));
describe("VerificationTokenService", () => {
const originalEnv = process.env;
beforeEach(() => {
vi.clearAllMocks();
process.env = { ...originalEnv };
process.env.NEXTAUTH_SECRET = "test-secret";
vi.mocked(VerificationTokenRepository.create).mockResolvedValue({
identifier: "[email protected]",
token: "hashed-token",
expires: new Date(),
} as any); // eslint-disable-line @typescript-eslint/no-explicit-any
});
afterEach(() => {
process.env = originalEnv;
});
describe("create", () => {
test("should create a verification token and return unhashed token", async () => {
const identifier = "[email protected]";
const expires = new Date(Date.now() + 86400 * 1000);
const result = await VerificationTokenService.create({
identifier,
expires,
});
expect(result).toBeTruthy();
expect(typeof result).toBe("string");
expect(result.length).toBeGreaterThan(0);
expect(VerificationTokenRepository.create).toHaveBeenCalled();
});
test("should call repository with correct identifier and expires", async () => {
const identifier = "[email protected]";
const expires = new Date("2025-12-31T23:59:59.000Z");
await VerificationTokenService.create({
identifier,
expires,
});
expect(VerificationTokenRepository.create).toHaveBeenCalledWith({
identifier,
token: expect.any(String),
expires,
});
const callArgs = vi.mocked(VerificationTokenRepository.create).mock.calls[0][0];
expect(callArgs.identifier).toBe(identifier);
expect(callArgs.expires).toBe(expires);
expect(callArgs.token).toBeTruthy();
});
test("should hash token with NEXTAUTH_SECRET", async () => {
const identifier = "[email protected]";
const expires = new Date(Date.now() + 86400 * 1000);
await VerificationTokenService.create({
identifier,
expires,
});
const callArgs = vi.mocked(VerificationTokenRepository.create).mock.calls[0][0];
// The hashed token should be a 64-character hex string (SHA-256)
expect(callArgs.token).toMatch(/^[a-f0-9]{64}$/);
});
test("should generate unique tokens on multiple calls", async () => {
const identifier = "[email protected]";
const expires = new Date(Date.now() + 86400 * 1000);
const result1 = await VerificationTokenService.create({
identifier,
expires,
});
const result2 = await VerificationTokenService.create({
identifier,
expires,
});
expect(result1).not.toBe(result2);
expect(VerificationTokenRepository.create).toHaveBeenCalledTimes(2);
});
test("should handle Date objects for expires", async () => {
const identifier = "[email protected]";
const expires = new Date(2025, 11, 31, 23, 59, 59);
await VerificationTokenService.create({
identifier,
expires,
});
expect(VerificationTokenRepository.create).toHaveBeenCalledWith({
identifier,
token: expect.any(String),
expires: expect.any(Date),
});
const callArgs = vi.mocked(VerificationTokenRepository.create).mock.calls[0][0];
expect(callArgs.expires).toBeInstanceOf(Date);
expect(callArgs.expires.getTime()).toBe(expires.getTime());
});
test("should generate 32-byte random token in hex format", async () => {
const identifier = "[email protected]";
const expires = new Date(Date.now() + 86400 * 1000);
const token = await VerificationTokenService.create({
identifier,
expires,
});
// 32 bytes = 64 hex characters
expect(token).toMatch(/^[a-f0-9]{64}$/);
});
});
});