149 lines
2.7 KiB
Markdown
149 lines
2.7 KiB
Markdown
|
|
# License Key Library
|
|
|
|
A simple, secure, and flexible licensing library built for Node.js, React, Next.js, and general JavaScript applications.
|
|
|
|
---
|
|
|
|
## 🚀 Quick Start
|
|
|
|
### Installation
|
|
|
|
```bash
|
|
npm install license-lib
|
|
```
|
|
|
|
|
|
Environment Setup
|
|
Create a .env file at the root with your database and JWT configuration:
|
|
|
|
env
|
|
```JWT_SECRET=your_jwt_secret
|
|
DB_HOST=localhost
|
|
DB_NAME=license_db
|
|
DB_USER_ADMIN=license_admin
|
|
DB_PASS_ADMIN=secure_admin_password
|
|
DB_USER_READER=license_reader
|
|
DB_PASS_READER=secure_reader_password
|
|
```
|
|
|
|
|
|
⚙️ Database Setup
|
|
|
|
Use the provided SQL script to set up your database:
|
|
|
|
```sql
|
|
CREATE DATABASE license_db;
|
|
|
|
USE license_db;
|
|
|
|
CREATE TABLE licenses (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
user_id VARCHAR(255) NOT NULL,
|
|
hashed_key VARCHAR(255) NOT NULL,
|
|
license_type ENUM('one-time', 'subscription', 'trial') DEFAULT 'one-time',
|
|
tier VARCHAR(50) DEFAULT 'basic',
|
|
valid_until DATETIME DEFAULT NULL,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
CREATE USER 'license_reader'@'%' IDENTIFIED BY 'secure_reader_password';
|
|
GRANT SELECT ON license_db.* TO 'license_reader'@'%';
|
|
|
|
CREATE USER 'license_admin'@'%' IDENTIFIED BY 'secure_admin_password';
|
|
GRANT SELECT, INSERT, UPDATE ON license_db.* TO 'license_admin'@'%';
|
|
|
|
FLUSH PRIVILEGES;
|
|
```
|
|
|
|
📦 API Endpoints
|
|
|
|
Generate License
|
|
|
|
POST /generate-license
|
|
|
|
Body:
|
|
|
|
```json
|
|
{
|
|
"userId": "user123",
|
|
"licenseType": "subscription",
|
|
"tier": "pro",
|
|
"validUntil": "2024-12-31"
|
|
}
|
|
```
|
|
Response:
|
|
|
|
```json
|
|
Copy code
|
|
{
|
|
"licenseKey": "generated.jwt.token"
|
|
}
|
|
```
|
|
|
|
Validate License
|
|
|
|
POST /validate-license
|
|
|
|
Body:
|
|
|
|
```json
|
|
Copy code
|
|
{
|
|
"userId": "user123",
|
|
"licenseKey": "generated.jwt.token"
|
|
}
|
|
```
|
|
Response:
|
|
|
|
```json
|
|
{
|
|
"valid": true
|
|
}
|
|
```
|
|
|
|
🎯 Client Integration
|
|
Import functions directly from the client module:
|
|
|
|
```javascript
|
|
import { activateLicense, generateFingerprint } from './client';
|
|
|
|
// Validate license
|
|
const valid = await activateLicense(userId, licenseKey);
|
|
|
|
// Device fingerprinting
|
|
const fingerprint = generateFingerprint();
|
|
```
|
|
|
|
⚛️ React Integration
|
|
Use the provided React hook and provider:
|
|
|
|
```jsx
|
|
import { LicenseProvider } from './react/LicenseProvider';
|
|
import useLicense from './react/useLicense';
|
|
|
|
const App = () => (
|
|
<LicenseProvider userId="user123" licenseKey="jwt.token">
|
|
<YourApp />
|
|
</LicenseProvider>
|
|
);
|
|
|
|
const YourApp = () => {
|
|
const { isLicensed } = useLicense();
|
|
return <div>{isLicensed ? 'Licensed!' : 'Not Licensed!'}</div>;
|
|
};
|
|
```
|
|
|
|
▲ Next.js Integration
|
|
Wrap your component easily:
|
|
|
|
```jsx
|
|
import withLicense from './nextjs/withLicense';
|
|
|
|
const Page = ({ isLicensed }) => (
|
|
<div>{isLicensed ? 'Licensed!' : 'Not Licensed!'}</div>
|
|
);
|
|
|
|
export default withLicense(Page, { userId: 'user123', licenseKey: 'jwt.token' });
|
|
```
|