// withUser.ts
import { customCtx } from 'convex-helpers/server/customFunctions'
import { zCustomMutation, zCustomQuery } from 'convex-helpers/server/zod'
import { QueryCtx, mutation, query } from '../_generated/server'
export async function getUserByTokenIdentifier<Ctx extends QueryCtx>(ctx: Ctx) {
const identity = await ctx.auth.getUserIdentity()
if (!identity) {
throw new Error('Unauthenticated call to function requiring authentication')
}
const user = await ctx.db
.query('user')
.withIndex('by_token', (q) => q.eq('tokenIdentifier', identity.tokenIdentifier))
.unique()
if (!user) throw new Error('User not found')
return user
}
const addUser = customCtx(async (ctx: QueryCtx) => ({
user: await getUserByTokenIdentifier(ctx),
}))
export const mutationWithUser = zCustomMutation(mutation, addUser)
export const queryWithUser = zCustomQuery(query, addUser)
// withUser.ts
import { customCtx } from 'convex-helpers/server/customFunctions'
import { zCustomMutation, zCustomQuery } from 'convex-helpers/server/zod'
import { QueryCtx, mutation, query } from '../_generated/server'
export async function getUserByTokenIdentifier<Ctx extends QueryCtx>(ctx: Ctx) {
const identity = await ctx.auth.getUserIdentity()
if (!identity) {
throw new Error('Unauthenticated call to function requiring authentication')
}
const user = await ctx.db
.query('user')
.withIndex('by_token', (q) => q.eq('tokenIdentifier', identity.tokenIdentifier))
.unique()
if (!user) throw new Error('User not found')
return user
}
const addUser = customCtx(async (ctx: QueryCtx) => ({
user: await getUserByTokenIdentifier(ctx),
}))
export const mutationWithUser = zCustomMutation(mutation, addUser)
export const queryWithUser = zCustomQuery(query, addUser)