@lohono/react-auth
v2.2.6
Published
Authentication component for Lohono applications
Readme
@lohono/react-auth
A lightweight authentication component library for React applications.
Installation
npm install @lohono/react-authFeatures
- User session management
- User information handling
- TypeScript support
- Support for both CommonJS and ES Modules
- Lightweight with minimal dependencies
Usage
Using Session Hook
import { useSession } from '@lohono/react-auth';
const AuthComponent = () => {
const { session, isAuthenticated, updateSession, clearSession } = useSession();
// Handle login
const handleLogin = (token) => {
updateSession(token);
};
// Handle logout
const handleLogout = () => {
clearSession();
};
return (
<div>
{isAuthenticated ? (
<button onClick={handleLogout}>Logout</button>
) : (
<button onClick={() => handleLogin("your-token")}>Login</button>
)}
</div>
);
};Using User Info Hook
import { useUserInfo } from '@lohono/react-auth';
const UserProfile = () => {
const { userInfo, error, updateUserInfo, clearUserInfo } = useUserInfo();
if (error) return <div>Error: {error.message}</div>;
if (!userInfo) return <div>No user info found</div>;
return (
<div>
<h1>Welcome, {userInfo.name}</h1>
<p>Email: {userInfo.email}</p>
<button onClick={clearUserInfo}>Clear Profile</button>
</div>
);
};API Reference
useSession Hook
session: Current session stringerror: Error object if any error occursupdateSession(newSession: string): Update sessionclearSession(): Clear current sessionisAuthenticated: Boolean indicating if session exists
useUserInfo Hook
userInfo: Current user information objecterror: Error object if any error occursupdateUserInfo(newUserInfo: UserInfo): Update user informationclearUserInfo(): Clear user information
Development
# Install dependencies
npm install
# Build the library
npm run buildRequirements
- React 16.8.0 or higher
- React DOM 16.8.0 or higher
License
MIT © Isprava Developer
