Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(auth): let invited users sign up with OAuth in invite-only mode

Andrej737ec6e79f85
diff --git a/apps/api/src/auth.ts b/apps/api/src/auth.ts
index be5676826..1183a11ad 100644
--- a/apps/api/src/auth.ts
+++ b/apps/api/src/auth.ts
@@ -67,6 +67,11 @@ function normalizeInvitationId(value: unknown): string | undefined {
return normalized;
}
+function isOAuthCallbackPath(path: unknown): boolean {
+ if (typeof path !== "string") return false;
+ return path.startsWith("/callback/") || path.startsWith("/oauth2/callback/");
+}
+
const apiUrl = process.env.KANEO_API_URL || "http://localhost:1337";
const clientUrl = process.env.KANEO_CLIENT_URL || "http://localhost:5173";
const isHttps = apiUrl.startsWith("https://");
@@ -540,6 +545,7 @@ export const auth = betterAuth({
const result = await checkRegistrationAllowed(
user.email,
invitationId,
+ { allowInvitationByEmail: isOAuthCallbackPath(ctx?.path) },
);
if (!result.allowed) {
throw new APIError("FORBIDDEN", {
diff --git a/apps/api/src/utils/check-registration-allowed.ts b/apps/api/src/utils/check-registration-allowed.ts
index 57839e5ad..b40aef210 100644
--- a/apps/api/src/utils/check-registration-allowed.ts
+++ b/apps/api/src/utils/check-registration-allowed.ts
@@ -19,6 +19,7 @@ type RegistrationCheckResult = {
export async function checkRegistrationAllowed(
email?: string,
invitationId?: string,
+ options?: { allowInvitationByEmail?: boolean },
): Promise<RegistrationCheckResult> {
const isRegistrationDisabled = process.env.DISABLE_REGISTRATION === "true";
@@ -29,7 +30,9 @@ export async function checkRegistrationAllowed(
};
}
- if (!invitationId) {
+ const canMatchByEmail = Boolean(options?.allowInvitationByEmail && email);
+
+ if (!invitationId && !canMatchByEmail) {
return {
allowed: false,
reason:
@@ -73,7 +76,7 @@ async function findValidInvitation(
conditions.push(eq(invitationTable.email, email.toLowerCase()));
}
- if (!invitationId) {
+ if (!invitationId && !email) {
return null;
}
diff --git a/tests/api-integration/registration-invitation.test.ts b/tests/api-integration/registration-invitation.test.ts
new file mode 100644
index 000000000..35929e34e
--- /dev/null
+++ b/tests/api-integration/registration-invitation.test.ts
@@ -0,0 +1,124 @@
+import { randomUUID } from "node:crypto";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import db, { schema } from "../../apps/api/src/database";
+import { checkRegistrationAllowed } from "../../apps/api/src/utils/check-registration-allowed";
+import { resetTestDatabase } from "./helpers/database";
+import { createWorkspaceMember } from "./helpers/fixtures";
+
+async function seedInvitation(
+ email: string,
+ overrides?: Partial<{ status: string; expiresAt: Date }>,
+) {
+ const inviter = await createWorkspaceMember({ role: "owner" });
+
+ const [invitation] = await db
+ .insert(schema.invitationTable)
+ .values({
+ workspaceId: inviter.workspace.id,
+ inviterId: inviter.user.id,
+ email: email.toLowerCase(),
+ role: "member",
+ status: overrides?.status ?? "pending",
+ expiresAt: overrides?.expiresAt ?? new Date(Date.now() + 86_400_000),
+ })
+ .returning();
+
+ return invitation;
+}
+
+describe("API integration: invite-only registration", () => {
+ beforeEach(async () => {
+ await resetTestDatabase();
+ process.env.DISABLE_REGISTRATION = "true";
+ });
+
+ afterEach(() => {
+ process.env.DISABLE_REGISTRATION = "false";
+ });
+
+ it("allows an OAuth signup whose verified email has a pending invitation", async () => {
+ const email = `invited-${randomUUID()}@example.com`;
+ const invitation = await seedInvitation(email);
+
+ const result = await checkRegistrationAllowed(email, undefined, {
+ allowInvitationByEmail: true,
+ });
+
+ expect(result.allowed).toBe(true);
+ expect(result.invitation?.id).toBe(invitation.id);
+ });
+
+ it("matches the invitation regardless of email casing", async () => {
+ const email = `invited-${randomUUID()}@example.com`;
+ await seedInvitation(email);
+
+ const result = await checkRegistrationAllowed(
+ email.toUpperCase(),
+ undefined,
+ { allowInvitationByEmail: true },
+ );
+
+ expect(result.allowed).toBe(true);
+ });
+
+ it("rejects an OAuth signup with no invitation for that email", async () => {
+ await seedInvitation(`invited-${randomUUID()}@example.com`);
+
+ const result = await checkRegistrationAllowed(
+ `stranger-${randomUUID()}@example.com`,
+ undefined,
+ { allowInvitationByEmail: true },
+ );
+
+ expect(result.allowed).toBe(false);
+ });
+
+ it.each([
+ ["expired", { expiresAt: new Date(Date.now() - 86_400_000) }],
+ ["already accepted", { status: "accepted" }],
+ ["canceled", { status: "canceled" }],
+ ])(
+ "rejects an OAuth signup when the invitation is %s",
+ async (_label, overrides) => {
+ const email = `invited-${randomUUID()}@example.com`;
+ await seedInvitation(email, overrides);
+
+ const result = await checkRegistrationAllowed(email, undefined, {
+ allowInvitationByEmail: true,
+ });
+
+ expect(result.allowed).toBe(false);
+ },
+ );
+
+ it("still requires the invitation link on the password signup path", async () => {
+ const email = `invited-${randomUUID()}@example.com`;
+ await seedInvitation(email);
+
+ const result = await checkRegistrationAllowed(email);
+
+ expect(result.allowed).toBe(false);
+ expect(result.reason).toContain("valid invitation link");
+ });
+
+ it("keeps rejecting an unknown invitation id", async () => {
+ const email = `invited-${randomUUID()}@example.com`;
+ await seedInvitation(email);
+
+ const result = await checkRegistrationAllowed(email, "does-not-exist", {
+ allowInvitationByEmail: true,
+ });
+
+ expect(result.allowed).toBe(false);
+ });
+
+ it("allows any signup once registration is open again", async () => {
+ process.env.DISABLE_REGISTRATION = "false";
+
+ const result = await checkRegistrationAllowed(
+ `anyone-${randomUUID()}@example.com`,
+ );
+
+ expect(result.allowed).toBe(true);
+ });
+});