Files
drive/packages/convex/functions.ts

110 lines
2.8 KiB
TypeScript
Raw Permalink Normal View History

2025-10-20 00:17:41 +00:00
import type { DataModel } from "@fileone/convex/dataModel"
import type { MutationCtx, QueryCtx } from "@fileone/convex/server"
import { mutation, query } from "@fileone/convex/server"
import type {
DocumentByName,
TableNamesInDataModel,
UserIdentity,
} from "convex/server"
2025-10-20 00:17:41 +00:00
import { type GenericId, v } from "convex/values"
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"
2025-10-20 00:17:41 +00:00
import * as ApiKey from "./model/apikey"
import { type AuthUser, userIdentityOrThrow, userOrThrow } from "./model/user"
import { ErrorCode, error } from "./shared/error"
2025-09-15 21:44:41 +00:00
export type AuthenticatedQueryCtx = QueryCtx & {
user: AuthUser
identity: UserIdentity
}
export type AuthenticatedMutationCtx = MutationCtx & {
user: AuthUser
identity: UserIdentity
}
2025-10-21 23:45:04 +00:00
export type ApiKeyAuthenticatedQueryCtx = QueryCtx & {
__branded: "ApiKeyAuthenticatedQueryCtx"
}
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 }
}),
)
2025-10-21 23:45:04 +00:00
/**
* Custom query that requires api key authentication for a query.
*/
export const apiKeyAuthenticatedQuery = customQuery(query, {
args: {
apiKey: v.string(),
},
input: async (ctx, args) => {
if (!(await ApiKey.verifyApiKey(ctx, args.apiKey))) {
error({
code: ErrorCode.Unauthenticated,
message: "Invalid API key",
})
2025-10-21 23:45:04 +00:00
}
return { ctx: ctx as ApiKeyAuthenticatedQueryCtx, args }
},
})
2025-10-20 00:17:41 +00:00
/**
* Custom mutation that requires api key authentication for a mutation.
*/
export const apiKeyAuthenticatedMutation = customMutation(mutation, {
args: {
apiKey: v.string(),
},
input: async (ctx, args) => {
if (!(await ApiKey.verifyApiKey(ctx, args.apiKey))) {
error({
code: ErrorCode.Unauthenticated,
message: "Invalid API key",
})
2025-10-20 00:17:41 +00:00
}
return { ctx, args }
},
})
/**
* 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
}