@seda-protocol/auth-core
v0.1.0
Published
Shared `seda-auth` domain types, API contracts, and Privy authentication helpers for React applications.
Readme
@seda-protocol/auth-core
Shared seda-auth domain types, API contracts, and Privy authentication helpers for React applications.
Entry points
@seda-protocol/auth-coreexports domain types and service API contracts.@seda-protocol/auth-core/clientexports the consumer HTTP client.@seda-protocol/auth-core/auth-uiexports reusable Privy authentication helpers for React applications.
Install the peer dependencies required by the entry points your application uses:
@seda-protocol/auth-core:effectand@effect/platform.@seda-protocol/auth-core/client:effect.@seda-protocol/auth-core/auth-ui:@privy-io/react-auth,reactandreact-dom(peer dependency of@privy-io/react-auth).
Client
TBD - not implemented yet
Auth UI
import {
SedaAuthPrivyProvider,
useSedaAuth,
} from "@seda-protocol/auth-core/auth-ui";
const loadMe = (accessToken: string) => {
return getMe(accessToken);
};
const classifyError = (error: unknown) => {
if (error instanceof ApiError && error.status === 401) {
return "login-again" as const;
}
if (error instanceof ApiError && error.status === 403) {
return "denied" as const;
}
return "error" as const;
};
function Dashboard() {
const session = useSedaAuth({
loadMe,
classifyError,
});
switch (session.status) {
case "loading":
return <p>Loading...</p>;
case "unauthenticated":
return <button onClick={session.login}>Log in</button>;
case "login-again":
return <button onClick={session.login}>Log in again</button>;
case "denied":
return (
<>
<p>You do not have access to this application.</p>
<button onClick={session.logout}>Log out</button>
</>
);
case "error":
return <p>Unable to load your session.</p>;
case "authorized":
return (
<>
<p>{session.me.user.email}</p>
<button onClick={session.logout}>Log out</button>
</>
);
}
}
export function App() {
return (
<SedaAuthPrivyProvider appId={privyAppId}>
<Dashboard />
</SedaAuthPrivyProvider>
);
}useSedaAuth exposes authentication actions and a discriminated session state with the following statuses:
loadingunauthenticatedauthorizeddeniedlogin-againerror
Consumers provide exchangeToken, which exchanges the current Privy identity token for a short-lived SEDA JWT, loadMe, which loads the authenticated SEDA user, and classifyError, which maps application-specific request errors to session states.
