UNPKG

@lens-protocol/react

Version:

Interacting with the Lens Protocol API using React.

141 lines (140 loc) 3.63 kB
import { UnspecifiedError, Profile } from '@lens-protocol/api-bindings'; import { LogoutReason, SessionType } from '@lens-protocol/domain/use-cases/authentication'; import { EvmAddress } from '@lens-protocol/shared-kernel'; import { ReadResult } from "../helpers/reads.js"; import { SuspenseEnabled, SuspenseResult } from "../helpers/suspense.js"; export { LogoutReason, SessionType }; /** * A not authenticated user's session */ export type AnonymousSession = { /** * The union discriminant. */ type: SessionType.Anonymous; /** * Whether the user is authenticated or not. */ authenticated: false; /** * The reason the previous session ended. */ lastLogoutReason?: LogoutReason; }; /** * A typical authenticated user's session. */ export type ProfileSession = { /** * The union discriminant. */ type: SessionType.WithProfile; /** * Whether the user is authenticated or not. */ authenticated: true; /** * The Profile Owner or an authorized Profile Manager. */ address: EvmAddress; /** * The authenticated Profile. */ profile: Profile; }; /** * An authenticated user's session with just a wallet address. * * This is currently not used, but will be used in the future. */ export type WalletOnlySession = { /** * The union discriminant. */ type: SessionType.JustWallet; /** * Whether the user is authenticated or not. */ authenticated: true; /** * The Profile Owner or an authorized Profile Manager. */ address: EvmAddress; }; /** * Describes the details of a user's session. */ export type Session = AnonymousSession | ProfileSession | WalletOnlySession; /** * {@link useSession} hook arguments */ export type UseSessionArgs = SuspenseEnabled; /** * Returns current {@link Session} data. * * Use this hook to determine if the user is authenticated or not. * ```tsx * function Page() { * const { data, error, loading } = useSession(); * * if (loading) return <Loader />; * * 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>; * } * } * ``` * * @category Authentication * @group Hooks */ export declare function useSession(args: UseSessionArgs): SuspenseResult<Session>; /** * Returns current {@link Session} data. * * This signature supports [React Suspense](https://react.dev/reference/react/Suspense). * * ```tsx * 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. * * @category Authentication * @group Hooks */ export declare function useSession(): ReadResult<Session, UnspecifiedError>;