meir-auth-lib
v1.0.1
Published
Generic, modular auth library for any frontend/backend
Readme
meir-auth-lib
🧠 A simple, modular authentication library for frontend and backend apps.
Built with React Context + localStorage and supports custom logic like registration.
🚀 Features
- ✅ React
AuthProvider+useAuthhook - ✅ In-memory + localStorage token management
- ✅ Supports login, logout, register
- ✅ Designed for frontend OR backend integration
- ✅ Fully tested with Jest + React Testing Library
- ✅ Framework-agnostic token service
📦 Installation
npm install meir-auth-libYou must also have
reactinstalled as a peer dependency.
🛠️ Usage
Wrap your app with AuthProvider
import React from 'react'
import ReactDOM from 'react-dom'
import { AuthProvider } from 'meir-auth-lib'
import App from './App'
ReactDOM.render(
<AuthProvider>
<App />
</AuthProvider>,
document.getElementById('root')
)Access auth from useAuth hook
import { useAuth } from 'meir-auth-lib'
const MyComponent = () => {
const { login, logout, token, isAuthenticated } = useAuth()
return (
<div>
{isAuthenticated ? 'Welcome!' : 'Please log in'}
<button onClick={() => login({ token: 'abc123' })}>Login</button>
<button onClick={logout}>Logout</button>
</div>
)
}📚 API Reference
🔐 useAuth()
React hook for accessing auth context.
const {
token,
isAuthenticated,
login, // ({ token }) => void
logout // () => void
} = useAuth()🧩 AuthProvider
Wraps your component tree and manages authentication state internally. Required for useAuth() to work.
<AuthProvider>
<YourApp />
</AuthProvider>⚙️ authService
A plain JS service (no React) to manage token and register new users.
authService.saveToken(token)
Stores a token in memory and localStorage.
authService.getToken()
Returns the token from memory or localStorage.
authService.clearToken()
Removes the stored token.
authService.login({ token })
Shortcut to call saveToken.
authService.logout()
Shortcut to call clearToken.
authService.register({ username, email, password }, axiosInstance)
Registers a new user using a provided axios instance:
await authService.register({
username: 'meir',
email: '[email protected]',
password: '123456'
}, axiosInstance)🔥 Throws if axiosInstance is missing.
🧪 Running Tests
npm run testTests are written using Jest + jsdom + React Testing Library
🔧 Build
npm run buildUses Vite for library bundling.
📁 Project Structure
meir-auth-lib/
├── src/
│ ├── AuthProvider.jsx
│ ├── authService.js
│ └── index.js
├── tests/
│ ├── AuthProvider.test.jsx
│ └── authService.test.js
├── package.json
├── vite.config.js
├── babel.config.js
├── jest.config.js
└── README.md📜 License
MIT © Gazit
🙌 Contributing
PRs and suggestions are welcome!
Please open an issue or fork this repo and submit a pull request.
