• useSession is a hook that lets you access the current Session

    Parameters

    Returns SuspenseResult<Session>

    Example

    const { data, error, loading } = useSession();
    

    Basic Usage

    Use this hook to determine if the user is authenticated or not.

    function Page() {
    const { data, error, loading } = useSession();

    if (loading) return <p>Loading...</p>;

    if (error) return <p>Something went wrong.</p>;

    switch (data.type) {
    case SessionType.Anonymous:
    // data is a AnonymousSession
    return <Login />;

    case SessionType.JustWallet:
    // data is a WalletOnlySession
    return <MyWallet address={data.address} />;

    case SessionType.WithProfile:
    // data is a ProfileSession
    return <MyProfile profile={data.profile} />;

    default:
    return <p>Something went wrong.</p>;
    }
    }

    Suspense Enabled

    You can enable suspense mode to suspend the component until the session data is available.

    function Page() {
    const { data } = useSession({ suspense: true });

    switch (data.type) {
    case SessionType.Anonymous:
    // data is a AnonymousSession
    return <Login />;

    case SessionType.JustWallet:
    // data is a WalletOnlySession
    return <MyWallet address={data.address} />;

    case SessionType.WithProfile:
    // data is a ProfileSession
    return <MyProfile profile={data.profile} />;

    default:
    return <p>Something went wrong.</p>;
    }
    }

    Further session data updates will NOT trigger a suspense.

  • Parameters

    Returns ReadResult<Session, UnspecifiedError>