@painda/auth
v0.1.2
Published
PaindaProtocol authentication middleware and JWT validators
Maintainers
Readme
@painda/auth
Authentication middleware for PaindaProtocol.
Token-based auth with configurable validators, guest access, and timeout handling.
Part of the PaindaProtocol ecosystem.
Installation
npm install @painda/auth @painda/coreQuick Start
import { PPServer } from '@painda/core';
import { PPAuthMiddleware } from '@painda/auth';
const server = new PPServer({ port: 3000 });
const auth = new PPAuthMiddleware(server, {
validator: async (token) => {
// Validate JWT, API key, etc.
const user = await verifyToken(token);
if (!user) throw new Error('Invalid token');
return user; // attached to socket.userContext
},
authTimeout: 5000, // 5s to authenticate
allowGuest: false, // require authentication
});
server.on('connection', (client) => {
if (auth.isAuthenticated(client)) {
// Client is verified
const user = (client as any).userContext;
console.log('Authenticated:', user.name);
}
});Options
| Option | Default | Description |
|--------|---------|-------------|
| validator | required | (token) => user \| boolean |
| authTimeout | 5000 | Ms before auto-disconnect |
| allowGuest | false | Allow connections without token |
Flow
- Client connects → auth timer starts
- Client sends
{ type: "authenticate", payload: { token } } - Validator runs → success:
auth-success/ fail: disconnect with4002 - Timeout → disconnect with
4001
License
MIT
