winsoroaks
winsoroaks•9mo ago

can i do auth or run a function on the server side?

hi all! i have the following code,
"use client"

import { api } from "@/convex/_generated/api"
import { useConvexAuth, useQuery } from "convex/react"

import { Skeleton } from "@/components/ui/skeleton"

export function SideBar() {
const { isLoading } = useConvexAuth()

const username = useQuery(
api.db.username,
isLoading ? "skip" : {}
)

return <span className="capitalize">{username}</span>
}
"use client"

import { api } from "@/convex/_generated/api"
import { useConvexAuth, useQuery } from "convex/react"

import { Skeleton } from "@/components/ui/skeleton"

export function SideBar() {
const { isLoading } = useConvexAuth()

const username = useQuery(
api.db.username,
isLoading ? "skip" : {}
)

return <span className="capitalize">{username}</span>
}
and i've been running into the following error when i click on "logout"
ConvexError: [CONVEX Q(db:username)] [Request ID: 26f4c2ab052920a5] Server Error
Uncaught ConvexError: Unauthenticated call to function requiring authentication
at getClerkIdentity (../../convex/db/user.ts:18:6)
at async handler (../../convex/db/user.ts:166:17)

Called by client
ConvexError: [CONVEX Q(db:username)] [Request ID: 26f4c2ab052920a5] Server Error
Uncaught ConvexError: Unauthenticated call to function requiring authentication
at getClerkIdentity (../../convex/db/user.ts:18:6)
at async handler (../../convex/db/user.ts:166:17)

Called by client
is it possible to move api.db.username to the server side for this component? or am i taking a wrong path? thanks!
2 Replies
ian
ian•9mo ago
That function is already server-side in Convex, and that error is being thrown there. When the user clicks logout, your client is clearing the authentication state, but not stopping the request to db:username. Is that component inside a <Authenticated> tag? e.g. <Authenticated><SideBar/></Authenticated>? Or try doing isAuthenticated instead of isLoading on the skip line
winsoroaks
winsoroaksOP•9mo ago
bingo! the isAuthenticated fixed it. thanks 🙂

Did you find this page helpful?