naystack - v1.5.10
    Preparing search index...

    Function useToken

    • Returns the current JWT access token from TokenContext. Must be used inside AuthWrapper.

      Common uses: conditional rendering based on auth state, passing to custom fetch headers.

      Returns string | null

      The access token string, or null if not yet loaded or user is logged out.

      import { useToken } from "naystack/auth/client";

      function Navbar() {
      const token = useToken();
      return <Link href={token ? "/dashboard" : "/login"}>{token ? "Dashboard" : "Log in"}</Link>;
      }
      const token = useToken();
      const response = await fetch("/api/custom", {
      headers: { Authorization: `Bearer ${token}` },
      });