2025-10-16 21:43:23 +00:00
|
|
|
import type {
|
|
|
|
|
DocumentByName,
|
|
|
|
|
TableNamesInDataModel,
|
|
|
|
|
UserIdentity,
|
|
|
|
|
} from "convex/server"
|
|
|
|
|
import type { GenericId } from "convex/values"
|
2025-09-15 21:44:41 +00:00
|
|
|
import {
|
2025-09-15 22:58:23 +00:00
|
|
|
customCtx,
|
2025-09-15 21:44:41 +00:00
|
|
|
customMutation,
|
|
|
|
|
customQuery,
|
|
|
|
|
} from "convex-helpers/server/customFunctions"
|
2025-10-16 21:43:23 +00:00
|
|
|
import type { DataModel } from "./_generated/dataModel"
|
2025-09-15 22:58:23 +00:00
|
|
|
import type { MutationCtx, QueryCtx } from "./_generated/server"
|
2025-09-15 21:44:41 +00:00
|
|
|
import { mutation, query } from "./_generated/server"
|
2025-10-05 22:14:44 +00:00
|
|
|
import { type AuthUser, userIdentityOrThrow, userOrThrow } from "./model/user"
|
2025-09-15 21:44:41 +00:00
|
|
|
|
2025-09-15 22:58:23 +00:00
|
|
|
export type AuthenticatedQueryCtx = QueryCtx & {
|
2025-10-05 22:14:44 +00:00
|
|
|
user: AuthUser
|
2025-09-15 22:58:23 +00:00
|
|
|
identity: UserIdentity
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type AuthenticatedMutationCtx = MutationCtx & {
|
2025-10-05 22:14:44 +00:00
|
|
|
user: AuthUser
|
2025-09-15 22:58:23 +00:00
|
|
|
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
|
|
|
|
|
*/
|
2025-09-15 22:58:23 +00:00
|
|
|
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)
|
2025-09-15 22:58:23 +00:00
|
|
|
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
|
|
|
|
|
*/
|
2025-09-15 22:58:23 +00:00
|
|
|
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)
|
2025-09-15 22:58:23 +00:00
|
|
|
return { user, identity }
|
|
|
|
|
}),
|
|
|
|
|
)
|
2025-10-16 21:43:23 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets a document by its id and checks if the user is authorized to access it
|
|
|
|
|
*
|
|
|
|
|
* @returns The document associated with the id or null if the document is not found.
|
|
|
|
|
*/
|
|
|
|
|
export async function authorizedGet<T extends TableNamesInDataModel<DataModel>>(
|
|
|
|
|
ctx: AuthenticatedQueryCtx | AuthenticatedMutationCtx,
|
|
|
|
|
id: GenericId<T>,
|
|
|
|
|
): Promise<DocumentByName<DataModel, T> | null> {
|
|
|
|
|
const item = await ctx.db.get(id)
|
|
|
|
|
if (item && item.userId !== ctx.user._id) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
return item
|
|
|
|
|
}
|