You can use the boolean
authenticated from the usePrivy hook to determine if your user is authenticated or not.Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Check if a user is authenticated using the usePrivy hook and gate UI experiences based on auth status
authenticated from the usePrivy hook to determine if your user is authenticated or not.authenticated: boolean;
readyimport { useRouter } from "next/router";
import { usePrivy } from "@privy-io/react-auth";
export default function MyComponent() {
const { ready, authenticated, user } = usePrivy();
const router = useRouter();
if (!ready) {
// Do nothing while the PrivyProvider initializes with updated user state
return <></>;
}
if (ready && !authenticated) {
// Replace this code with however you'd like to handle an unauthenticated user
// As an example, you might redirect them to a login page
router.push("/login");
}
if (ready && authenticated) {
// Replace this code with however you'd like to handle an authenticated user
return <p>User {user?.id} is logged in.</p>;
}
}
Was this page helpful?
