feat: add auth to convex fns

This commit is contained in:
2025-09-15 21:44:41 +00:00
parent f06064fc81
commit 8916142e04
10 changed files with 166 additions and 33 deletions

39
convex/functions.ts Normal file
View File

@@ -0,0 +1,39 @@
import {
customMutation,
customQuery,
} from "convex-helpers/server/customFunctions"
import { mutation, query } from "./_generated/server"
import { userIdentityOrThrow, userOrThrow } from "./model/user"
/**
* Custom query that automatically provides authenticated user context
* Throws an error if the user is not authenticated
*/
export const authenticatedQuery = customQuery(query, {
args: {},
input: async (ctx, args) => {
const user = await userOrThrow(ctx)
const identity = await userIdentityOrThrow(ctx)
return {
ctx: { ...ctx, user, identity },
args,
}
},
})
/**
* Custom mutation that automatically provides authenticated user context
* Throws an error if the user is not authenticated
*/
export const authenticatedMutation = customMutation(mutation, {
args: {},
input: async (ctx, args) => {
const user = await userOrThrow(ctx)
const identity = await userIdentityOrThrow(ctx)
return {
ctx: { ...ctx, user, identity },
args,
}
},
})