You can use the boolean authenticated from the usePrivy hook to determine if your user is authenticated or not.Before determining a user’s auth status from Privy, you should verify that
Privy has fully initialized and is ready
Usage
import { 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>;
}
}