Files
lemonspace_app/convex/helpers.ts
Matthias 824939307c feat: enhance canvas functionality with proximity edge handling and improved authentication logging
- Introduced temporary edge styling for proximity connections in the canvas.
- Updated edge synchronization logic to filter out temporary edges during data processing.
- Enhanced authentication logging to provide detailed user identity information when authentication fails.
- Added a new utility function for sanitizing edge handle IDs to ensure compatibility with React Flow.
- Implemented a node handle mapping for proximity connections to streamline edge management.
2026-03-26 10:57:10 +01:00

44 lines
1.5 KiB
TypeScript

import { QueryCtx, MutationCtx } from "./_generated/server";
import { authComponent } from "./auth";
type SafeAuthUser = NonNullable<
Awaited<ReturnType<typeof authComponent.safeGetAuthUser>>
>;
/** Better-Auth-User mit für die App garantierter userId (Convex-_id als Fallback). */
export type AuthUser = Omit<SafeAuthUser, "userId"> & { userId: string };
/**
* Erfordert einen authentifizierten User und gibt dessen userId zurück.
* Wirft einen Error wenn nicht eingeloggt — für Mutations und geschützte Queries.
*/
export async function requireAuth(
ctx: QueryCtx | MutationCtx
): Promise<AuthUser> {
const user = await authComponent.safeGetAuthUser(ctx);
if (!user) {
const identity = await ctx.auth.getUserIdentity();
console.error("[requireAuth] safeGetAuthUser returned null", {
hasIdentity: Boolean(identity),
identityIssuer: identity?.issuer ?? null,
identitySubject: identity?.subject ?? null,
});
throw new Error("Unauthenticated");
}
const userId = user.userId ?? String(user._id);
if (!userId) {
console.error("[requireAuth] safeGetAuthUser returned user without userId", {
userRecordId: String(user._id),
});
throw new Error("Unauthenticated");
}
return { ...user, userId };
}
/**
* Gibt den User zurück oder null — für optionale Auth-Checks (z.B. public Queries).
*/
export async function optionalAuth(ctx: QueryCtx | MutationCtx) {
return await authComponent.safeGetAuthUser(ctx);
}