@junaadh/aegis-react
v0.1.0
Published
React bindings for the Aegis auth server
Readme
@junaadh/aegis-react
React bindings for applications that use the Aegis auth server.
@junaadh/aegis-react wraps @junaadh/aegis with:
- a provider for auth state
- hooks for common auth actions
- a simple auth gate component
Install
bun add @junaadh/aegis-react @junaadh/aegisWrap your app
import { AegisProvider } from "@junaadh/aegis-react";
export function App() {
return (
<AegisProvider baseUrl="https://auth.example.com">
<Routes />
</AegisProvider>
);
}Use hooks
import { useLogin, useLogout, useUser } from "@junaadh/aegis-react";
export function AccountMenu() {
const user = useUser();
const login = useLogin();
const logout = useLogout();
if (!user) {
return (
<button
onClick={() =>
login({
email: "[email protected]",
password: "secret",
})
}
>
Sign in
</button>
);
}
return <button onClick={() => logout()}>Sign out {user.email}</button>;
}Read session state
import { useSession } from "@junaadh/aegis-react";
export function SessionDebug() {
const session = useSession();
return <pre>{JSON.stringify(session, null, 2)}</pre>;
}Auth gate
import { AuthGate } from "@junaadh/aegis-react";
export function ProtectedPage() {
return (
<AuthGate fallback={<div>Please sign in</div>}>
<Dashboard />
</AuthGate>
);
}Notes
- This package is client-side React code.
- It expects a reachable Aegis server
baseUrl. - Cookie-based auth works naturally in browser environments because the underlying client uses
credentials: "include"by default.
