Package Exports
- sdk-simple-auth
Readme
๐ SDK Simple Auth
Universal JavaScript/TypeScript authentication library with multi-backend support
๐ Key Features
- โ Multi-Backend: Compatible with Node.js/Express, Laravel Sanctum, standard JWT
- โ TypeScript: Full type support and native compatibility
- โ React Ready: Hooks and components ready to use
- โ Auto-detection: Automatically detects backend type
- โ Data Preservation: Maintains all original backend data
- โ Auto Refresh: Intelligent token management
- โ Flexible Storage: LocalStorage, IndexedDB, memory
- โ Zero Dependencies: No heavy external dependencies
๐ฆ Installation
npm install sdk-simple-auth๐ฏ Quick Start
Basic Configuration
import { AuthSDK } from 'sdk-simple-auth';
const auth = new AuthSDK({
authServiceUrl: 'http://localhost:3000'
});
// Login
const user = await auth.login({
email: 'user@example.com',
password: 'my-password'
});
console.log('Authenticated user:', user);With React
import React, { useEffect, useState } from 'react';
import { AuthSDK } from 'sdk-simple-auth';
function App() {
const [auth] = useState(() => new AuthSDK({
authServiceUrl: process.env.REACT_APP_API_URL
}));
const [user, setUser] = useState(null);
useEffect(() => {
// Check existing session
auth.isAuthenticated().then(isAuth => {
if (isAuth) {
setUser(auth.getCurrentUser());
}
});
// Listen to auth changes
const unsubscribe = auth.onAuthStateChanged((state) => {
setUser(state.user);
});
return unsubscribe;
}, [auth]);
const handleLogin = async () => {
try {
const user = await auth.login({
email: 'user@example.com',
password: 'password'
});
console.log('Login successful:', user);
} catch (error) {
console.error('Login error:', error);
}
};
return (
<div>
{user ? (
<div>
<h1>Hello, {user.name}!</h1>
<button onClick={() => auth.logout()}>
Logout
</button>
</div>
) : (
<button onClick={handleLogin}>
Login
</button>
)}
</div>
);
}๐ ๏ธ Backend Configuration
Node.js/Express
import { createQuickNodeAuth } from 'sdk-simple-auth';
const auth = createQuickNodeAuth('http://localhost:3000');
// Your backend should return:
// {
// "success": true,
// "data": {
// "user": { "id": 1, "email": "user@test.com", "name": "User" },
// "token": "jwt-token-here",
// "refreshToken": "refresh-token-here"
// }
// }Laravel Sanctum
import { createQuickSanctumAuth } from 'sdk-simple-auth';
const auth = createQuickSanctumAuth('http://localhost:8000/api');
const user = await auth.login({
email: 'user@example.com',
password: 'password',
device_name: 'my-web-app'
});
// Compatible with Sanctum responses:
// {
// "user": { "id": 1, "email": "user@test.com", "created_at": "..." },
// "token": "1|sanctum-token-here"
// }Standard JWT
import { AuthSDK } from 'sdk-simple-auth';
const auth = new AuthSDK({
authServiceUrl: 'http://localhost:3000',
backend: {
type: 'jwt-standard',
userSearchPaths: ['user', 'data.user'],
fieldMappings: {
userId: ['sub', 'id'],
email: ['email'],
name: ['name', 'username']
}
}
});๐ง Core API
Authentication Methods
// Login
const user = await auth.login(credentials);
// Register
const user = await auth.register(userData);
// Logout
await auth.logout();
// Check authentication
const isAuth = await auth.isAuthenticated();
// Get current user
const user = auth.getCurrentUser();
// Get valid token (with auto refresh)
const token = await auth.getValidAccessToken();
// Authorization headers
const headers = await auth.getAuthHeaders();
// { Authorization: 'Bearer jwt-token' }Token Management
// Manual refresh
const newTokens = await auth.refreshTokens();
// Force refresh
const tokens = await auth.forceRefreshTokens();
// Session information
const sessionInfo = await auth.getExtendedSessionInfo();
console.log({
isValid: sessionInfo.isValid,
expiresIn: sessionInfo.expiresIn,
canRefresh: sessionInfo.canRefresh
});Events and State
// Listen to state changes
const unsubscribe = auth.onAuthStateChanged((state) => {
console.log('State:', state.isAuthenticated);
console.log('User:', state.user);
console.log('Loading:', state.loading);
console.log('Error:', state.error);
});
// Get current state
const state = auth.getState();๐๏ธ Advanced Configuration
Complete Configuration
const auth = new AuthSDK({
authServiceUrl: 'http://localhost:3000',
// Custom endpoints
endpoints: {
login: '/auth/login',
register: '/auth/register',
refresh: '/auth/refresh',
logout: '/auth/logout',
profile: '/auth/profile'
},
// Storage configuration
storage: {
type: 'localStorage', // 'localStorage' | 'indexedDB'
tokenKey: 'access_token',
refreshTokenKey: 'refresh_token',
userKey: 'user_data'
},
// Auto refresh
tokenRefresh: {
enabled: true,
bufferTime: 300, // Refresh 5 min before expiry
maxRetries: 3
},
// Backend configuration
backend: {
type: 'node-express',
userSearchPaths: ['user', 'data.user'],
fieldMappings: {
userId: ['id', 'user_id'],
email: ['email'],
name: ['name', 'full_name']
},
preserveOriginalData: true
}
});๐งช Testing and Debugging
Debug Mode
// Analyze your API response
auth.debugResponse(response);
// Debug current token
auth.debugToken();
// Debug complete session
auth.debugSession();
// Test data extraction
auth.testExtraction(mockResponse);Backend Auto-detection
import { quickAnalyzeAndCreate } from 'sdk-simple-auth';
// Automatically analyzes response and creates SDK
const auth = quickAnalyzeAndCreate(
responseFromYourAPI,
'http://localhost:3000'
);๐ Error Handling
try {
const user = await auth.login(credentials);
} catch (error) {
if (error.message.includes('credentials')) {
console.log('Invalid credentials');
} else if (error.message.includes('network')) {
console.log('Network error');
} else {
console.log('Unknown error:', error.message);
}
}
// Listen to global errors
auth.onAuthStateChanged((state) => {
if (state.error) {
console.error('Authentication error:', state.error);
}
});๐ฑ Compatibility
- โ Browsers: Chrome, Firefox, Safari, Edge (ES2018+)
- โ Node.js: 14.x, 16.x, 18.x, 20.x
- โ Frameworks: React, Vue, Angular, Vanilla JS
- โ Bundlers: Webpack, Vite, Rollup, Parcel
- โ TypeScript: 4.5+
๐ Documentation
- ๐ Quick Start Guide
- ๐ง Advanced Configuration
- ๐ API Reference
- ๐งช Complete Examples
- ๐ Troubleshooting
๐ค Contributing
- Fork the repository
- Create a branch:
git checkout -b feature/new-feature - Commit:
git commit -am 'Add new feature' - Push:
git push origin feature/new-feature - Create Pull Request
๐ License
MIT License - see LICENSE for details.
๐ Support
- ๐ Complete documentation
- ๐ Report bugs
- ๐ฌ Discussions
Developed by olivio-git