2025-10-18 19:32:05 +00:00
|
|
|
import type { MutationCtx, QueryCtx } from "@fileone/convex/server"
|
2025-11-02 18:12:33 +00:00
|
|
|
import type { Doc } from "../_generated/dataModel"
|
2025-10-05 22:14:44 +00:00
|
|
|
import { authComponent } from "../auth"
|
2025-11-02 18:12:33 +00:00
|
|
|
import { type AuthenticatedQueryCtx, authorizedGet } from "../functions"
|
2025-10-18 14:02:20 +00:00
|
|
|
import * as Err from "../shared/error"
|
2025-09-15 21:44:41 +00:00
|
|
|
|
2025-10-05 22:14:44 +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) {
|
|
|
|
|
throw Err.create(Err.Code.Unauthenticated, "Not authenticated")
|
|
|
|
|
}
|
|
|
|
|
return identity
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-05 22:14:44 +00:00
|
|
|
* 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) {
|
2025-10-05 22:14:44 +00:00
|
|
|
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()
|
|
|
|
|
}
|