@winwinmbs/portal-auth-react
v1.0.0
Published
React bindings for @winwinmbs/portal-auth
Readme
@winwinmbs/portal-auth-react
React bindings for @winwinmbs/portal-auth. Provides AuthProvider, useAuth, and useSession hooks.
Install
npm install @winwinmbs/portal-auth @winwinmbs/portal-auth-reactRequires React 18 or 19 as a peer dependency.
Usage
// app root
import { AuthProvider } from '@winwinmbs/portal-auth-react';
<AuthProvider
config={{
apiUrl: 'https://api.winwinmbs.com',
mode: 'sso', // or 'same-origin' for the portal itself
portalUrl: 'https://portal.winwinmbs.com',
appId: 'your-app-id',
}}
onSessionExpired={() => router.replace('/login')}
onSessionRevoked={() => router.replace('/login?reason=revoked')}
>
<App />
</AuthProvider>// anywhere in the tree
import { useAuth, useSession } from '@winwinmbs/portal-auth-react';
const { user, isAuthenticated, login, logout } = useAuth();
const { sessionExpiring, remainingSeconds, continueSession } = useSession();
// SSO flow (external apps)
useEffect(() => {
const code = new URLSearchParams(location.search).get('code');
if (code) login.withSSO(code);
else if (!isAuthenticated) login.redirectToLogin();
}, [isAuthenticated, login]);
// Inactivity warning dialog
if (sessionExpiring) {
return <WarningDialog seconds={remainingSeconds} onContinue={continueSession} />;
}Hooks
useAuth()—{ user, isAuthenticated, isLoading, error, login: { withCredentials, withSSO, redirectToLogin, withOAuth }, logout }useSession()—{ remainingSeconds, sessionExpiring, continueSession, getAccessToken, lastActivityAt }useOAuth()— state for Google/LINE/Azure OAuth flows (optional)
License
MIT
