2025-09-15 21:44:41 +00:00
|
|
|
import type { MutationCtx, QueryCtx } from "../_generated/server"
|
2025-10-05 22:14:44 +00:00
|
|
|
import { authComponent } from "../auth"
|
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
|
|
|
|
|
}
|