Throughout your app, you may want to gate certain user experiences based on whether the current user is authenticated or not. Privy makes it easy to check your user’s authentication status and handle it appropriately.

You can use the boolean authenticated from the usePrivy hook to determine if your user is authenticated or not.

authenticated: boolean;

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>;
}
}

Was this page helpful?