Files
file-one/packages/convex/users.ts

33 lines
733 B
TypeScript
Raw Permalink Normal View History

2025-09-15 21:44:41 +00:00
import { mutation } from "./_generated/server"
import { authenticatedQuery } from "./functions"
import * as Err from "./model/error"
2025-09-13 22:02:27 +01:00
2025-09-15 21:44:41 +00:00
export const getCurrentUser = authenticatedQuery({
2025-09-13 22:02:27 +01:00
handler: async (ctx) => {
2025-09-15 21:44:41 +00:00
// ctx.user is the internal Convex user document
return ctx.user
},
})
export const syncUser = mutation({
handler: async (ctx) => {
const identity = await ctx.auth.getUserIdentity()
if (!identity) {
throw Err.create(Err.Code.Unauthenticated)
}
const existingUser = await ctx.db
.query("users")
.withIndex("byJwtSubject", (q) =>
q.eq("jwtSubject", identity.subject),
)
.first()
if (!existingUser) {
await ctx.db.insert("users", {
jwtSubject: identity.subject,
})
2025-09-15 21:44:41 +00:00
}
2025-09-13 22:02:27 +01:00
},
})