Files
drive/packages/convex/model/user.ts

38 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

import type { MutationCtx, QueryCtx } from "@fileone/convex/server"
2025-11-02 18:12:33 +00:00
import type { Doc } from "../_generated/dataModel"
import { authComponent } from "../auth"
2025-11-02 18:12:33 +00:00
import { type AuthenticatedQueryCtx, authorizedGet } from "../functions"
import { ErrorCode, error } from "../shared/error"
2025-09-15 21:44:41 +00:00
export type AuthUser = Awaited<ReturnType<typeof authComponent.getAuthUser>>
2025-09-15 21:44:41 +00:00
/**
* Get the current authenticated user identity
* Throws an error if the user is not authenticated */
export async function userIdentityOrThrow(ctx: QueryCtx | MutationCtx) {
const identity = await ctx.auth.getUserIdentity()
if (!identity) {
error({
code: ErrorCode.Unauthenticated,
message: "Not authenticated",
})
2025-09-15 21:44:41 +00:00
}
return identity
}
/**
* Get user document from JWT authentication
2025-09-15 21:44:41 +00:00
* Throws an error if the user is not authenticated
*/
export async function userOrThrow(ctx: QueryCtx | MutationCtx) {
const user = await authComponent.getAuthUser(ctx)
2025-09-15 21:44:41 +00:00
return user
}
2025-11-02 18:12:33 +00:00
export async function queryInfo(ctx: AuthenticatedQueryCtx) {
return await ctx.db
.query("userInfo")
.withIndex("byUserId", (q) => q.eq("userId", ctx.user._id))
.first()
}