lightning-auth-and-payment
Version:
Lightning Network authentication and payment processing library for modern web applications
160 lines (129 loc) • 4.09 kB
Markdown
# Next.js Lightning Authentication Example
This example demonstrates how to use `lightning-auth-and-payment` with Next.js using the automatic plugin system.
## Features
- ⚡ **Automatic Plugin Integration** - No manual API route creation
- 🔧 **Auto-Generated API Routes** - Authentication and payment endpoints
- 🗄️ **Automatic Prisma Schema** - Complete database schema generation
- 💳 **BTCPay Server Integration** - Lightning payments out of the box
- 📱 **Mobile Wallet Support** - QR code authentication
- 🚀 **Development Server** - HTTPS development with certificates
## Quick Start
1. **Install dependencies:**
```bash
npm install
```
2. **Configure environment:**
```bash
cp .env.example .env.local
# Edit .env.local with your configuration
```
3. **Initialize database:**
```bash
npx prisma migrate dev
npx prisma generate
```
4. **Start development server:**
```bash
npm run dev:lightning
```
5. **Open the application:**
- HTTPS: https://localhost:3443 (for Lightning wallets)
- HTTP: http://localhost:3000 (for development)
## What You Get Automatically
### 🔧 Generated API Routes
- `/api/auth/lnurl` - Generate Lightning authentication URL
- `/api/auth/callback` - Handle authentication callback
- `/api/auth/status` - Check authentication status
- `/api/auth/logout` - User logout
- `/api/user` - Get current user information
- `/api/payment/create-invoice` - Create Lightning invoice
- `/api/payment/status` - Check payment status
- `/api/webhooks/btcpay` - BTCPay Server webhooks
### 🗄️ Generated Database Schema
- User management
- Session handling
- LNURL challenges
- Payment tracking
- Order management
- Invoice management
### 🚀 Development Features
- HTTPS development server with SSL certificates
- Automatic schema generation
- Hot reloading
- TypeScript support
## Configuration
### Required Environment Variables
```env
# Session Management
SESSION_SECRET=your-super-secret-key-here
SESSION_COOKIE_DOMAIN=localhost
# Database
DATABASE_URL="file:./prisma/dev.db"
```
### Optional BTCPay Server Variables
```env
# BTCPay Server Integration
BTCPAY_HOST=https://your-btcpay-server.com
BTCPAY_STORE_ID=your-store-id
BTCPAY_API_KEY=your-api-key
BTCPAY_WEBHOOK_SECRET=your-webhook-secret
```
## Usage Examples
### Authentication Hook
```tsx
import { useLightningAuth } from 'lightning-auth-and-payment';
export default function MyComponent() {
const {
user,
isAuthenticated,
isLoading,
handleLogin,
showAuthModal,
setShowAuthModal
} = useLightningAuth();
return (
<div>
{isAuthenticated ? (
<p>Welcome, {user?.lnPubkey}!</p>
) : (
<button onClick={handleLogin}>
Login with Lightning
</button>
)}
</div>
);
}
```
### Payment Modal
```tsx
import { BTCPayPaymentModal } from 'lightning-auth-and-payment';
export default function PaymentComponent() {
return (
<BTCPayPaymentModal
isOpen={showModal}
amount={1000} // 1000 sats
description="Test payment"
metadata={{ orderId: '123' }}
onClose={() => setShowModal(false)}
onPaymentSuccess={() => console.log('Payment successful!')}
onPaymentError={(error) => console.error('Payment failed:', error)}
/>
);
}
```
## Development Commands
- `npm run dev` - Standard Next.js development
- `npm run dev:lightning` - HTTPS development with Lightning support
- `npm run build` - Production build
- `npm run start` - Production server
## Production Deployment
1. **Set up BTCPay Server** (recommended)
2. **Configure environment variables**
3. **Deploy to your hosting platform**
4. **Run database migrations**
The plugin automatically generates all necessary API routes and database schema, making deployment straightforward.
## Key Benefits
- **Zero Configuration** - Everything works out of the box
- **Automatic Updates** - Schema and routes stay up-to-date
- **Production Ready** - Complete Lightning payment system
- **Developer Friendly** - Hot reloading and TypeScript support