feat: add auth to convex fns
This commit is contained in:
39
convex/functions.ts
Normal file
39
convex/functions.ts
Normal 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,
|
||||
}
|
||||
},
|
||||
})
|
Reference in New Issue
Block a user