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

View File

@@ -1,10 +1,25 @@
import type { Id } from "./_generated/dataModel"
import { query } from "./_generated/server"
import { mutation } from "./_generated/server"
import { authenticatedQuery } from "./functions"
import { getOrCreateUser, userIdentityOrThrow } from "./model/user"
export const getCurrentUser = query({
export const getCurrentUser = authenticatedQuery({
handler: async (ctx) => {
return await ctx.db.get(
"j574n657f521n19v1stnr88ysd7qhbs1" as Id<"users">,
)
// ctx.user is the internal Convex user document
return ctx.user
},
})
export const syncUser = mutation({
handler: async (ctx) => {
// This function creates or updates the internal user from identity provider
const userId = await getOrCreateUser(ctx)
const identity = await userIdentityOrThrow(ctx)
return {
userId,
jwtSubject: identity.subject,
name: identity.name,
email: identity.email,
}
},
})