mirror of
https://github.com/get-drexa/drive.git
synced 2025-12-01 14:01:40 +00:00
- Replace Err.Code with ErrorCode throughout convex model files - Update error() function calls to use new signature - Remove unused Err namespace imports Co-authored-by: Ona <no-reply@ona.com>
38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import type { MutationCtx, QueryCtx } from "@fileone/convex/server"
|
|
import type { Doc } from "../_generated/dataModel"
|
|
import { authComponent } from "../auth"
|
|
import { type AuthenticatedQueryCtx, authorizedGet } from "../functions"
|
|
import { ErrorCode, error } from "../shared/error"
|
|
|
|
export type AuthUser = Awaited<ReturnType<typeof authComponent.getAuthUser>>
|
|
|
|
/**
|
|
* 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",
|
|
})
|
|
}
|
|
return identity
|
|
}
|
|
|
|
/**
|
|
* Get user document from JWT authentication
|
|
* Throws an error if the user is not authenticated
|
|
*/
|
|
export async function userOrThrow(ctx: QueryCtx | MutationCtx) {
|
|
const user = await authComponent.getAuthUser(ctx)
|
|
return user
|
|
}
|
|
|
|
export async function queryInfo(ctx: AuthenticatedQueryCtx) {
|
|
return await ctx.db
|
|
.query("userInfo")
|
|
.withIndex("byUserId", (q) => q.eq("userId", ctx.user._id))
|
|
.first()
|
|
}
|