Files
file-one/packages/convex/functions.ts

47 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

import type { UserIdentity } from "convex/server"
2025-09-15 21:44:41 +00:00
import {
customCtx,
2025-09-15 21:44:41 +00:00
customMutation,
customQuery,
} from "convex-helpers/server/customFunctions"
import type { Doc } from "./_generated/dataModel"
import type { MutationCtx, QueryCtx } from "./_generated/server"
2025-09-15 21:44:41 +00:00
import { mutation, query } from "./_generated/server"
import { userIdentityOrThrow, userOrThrow } from "./model/user"
export type AuthenticatedQueryCtx = QueryCtx & {
user: Doc<"users">
identity: UserIdentity
}
export type AuthenticatedMutationCtx = MutationCtx & {
user: Doc<"users">
identity: UserIdentity
}
2025-09-15 21:44:41 +00:00
/**
* Custom query that automatically provides authenticated user context
* Throws an error if the user is not authenticated
*/
export const authenticatedQuery = customQuery(
query,
customCtx(async (ctx: QueryCtx) => {
2025-09-15 21:44:41 +00:00
const user = await userOrThrow(ctx)
const identity = await userIdentityOrThrow(ctx)
return { user, identity }
}),
)
2025-09-15 21:44:41 +00:00
/**
* Custom mutation that automatically provides authenticated user context
* Throws an error if the user is not authenticated
*/
export const authenticatedMutation = customMutation(
mutation,
customCtx(async (ctx: MutationCtx) => {
2025-09-15 21:44:41 +00:00
const user = await userOrThrow(ctx)
const identity = await userIdentityOrThrow(ctx)
return { user, identity }
}),
)