@mesob/auth-hono 0.0.7 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +172 -0
- package/dist/index-CScADcDn.d.ts +137 -0
- package/dist/index-ULpI-i0z.d.ts +2008 -0
- package/dist/index.d.ts +23 -15
- package/dist/index.js +5272 -1885
- package/dist/index.js.map +1 -1
- package/dist/lib/cleanup.d.ts +11 -0
- package/dist/lib/cleanup.js +296 -0
- package/dist/lib/cleanup.js.map +1 -0
- package/dist/lib/cookie.d.ts +18 -0
- package/dist/lib/cookie.js +40 -0
- package/dist/lib/cookie.js.map +1 -0
- package/dist/lib/crypto.js +5 -0
- package/dist/lib/crypto.js.map +1 -1
- package/dist/lib/docs.d.ts +11 -0
- package/dist/lib/docs.js +14 -0
- package/dist/lib/docs.js.map +1 -0
- package/dist/lib/normalize-user.d.ts +32 -0
- package/dist/lib/normalize-user.js +15 -0
- package/dist/lib/normalize-user.js.map +1 -0
- package/dist/lib/openapi-config.d.ts +31 -0
- package/dist/lib/openapi-config.js +66 -0
- package/dist/lib/openapi-config.js.map +1 -0
- package/dist/lib/session.d.ts +43 -18
- package/dist/lib/session.js +57 -23
- package/dist/lib/session.js.map +1 -1
- package/dist/lib/tenant.d.ts +8 -1
- package/dist/lib/tenant.js +5 -4
- package/dist/lib/tenant.js.map +1 -1
- package/package.json +7 -4
- package/dist/index-J2ZbOnZO.d.ts +0 -76
- package/dist/lib/jwt.d.ts +0 -15
- package/dist/lib/jwt.js +0 -25
- package/dist/lib/jwt.js.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# @mesob/auth-hono
|
|
2
|
+
|
|
3
|
+
Complete Identity and Access Management (IAM) system for Hono applications with type-safe APIs, OpenAPI documentation, and RPC client support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔐 **Complete Authentication**: Sign up, sign in, sign out with email/phone
|
|
8
|
+
- 👤 **User Management**: Profile updates, email/phone changes
|
|
9
|
+
- 🔑 **Password Management**: Forgot, reset, change, verify
|
|
10
|
+
- ✉️ **Email Verification**: Request and confirm email verification
|
|
11
|
+
- 📱 **Phone Verification**: OTP-based phone verification
|
|
12
|
+
- 🏢 **Multi-Tenant Support**: Tenant isolation with configurable setup
|
|
13
|
+
- 👥 **IAM System**: Users, tenants, domains, roles, permissions management
|
|
14
|
+
- 📚 **OpenAPI Docs**: Interactive API documentation with Scalar UI
|
|
15
|
+
- 🔷 **Type-Safe RPC**: Generate type-safe clients for frontend
|
|
16
|
+
- 🛡️ **Type Safety**: Full TypeScript support with Hono typed context
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
### Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm add @mesob/auth-hono
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Basic Setup
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { mesobAuth } from '@mesob/auth-hono';
|
|
30
|
+
|
|
31
|
+
const auth = mesobAuth({
|
|
32
|
+
connectionString: process.env.DATABASE_URL!,
|
|
33
|
+
secret: process.env.AUTH_SECRET!,
|
|
34
|
+
title: 'My App IAM API',
|
|
35
|
+
docsTheme: 'saturn',
|
|
36
|
+
enableTenant: true,
|
|
37
|
+
session: {
|
|
38
|
+
expiresIn: '30d',
|
|
39
|
+
maxPerUser: 5,
|
|
40
|
+
},
|
|
41
|
+
email: {
|
|
42
|
+
enabled: true,
|
|
43
|
+
verificationRequired: true,
|
|
44
|
+
verificationExpiresIn: '1h',
|
|
45
|
+
resend: {
|
|
46
|
+
apiKey: process.env.RESEND_API_KEY!,
|
|
47
|
+
from: 'noreply@example.com',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
phone: {
|
|
51
|
+
enabled: true,
|
|
52
|
+
verificationRequired: true,
|
|
53
|
+
otpLength: 6,
|
|
54
|
+
otpExpiresIn: '10m',
|
|
55
|
+
smsConfig: {
|
|
56
|
+
baseUrl: process.env.SMS_BASE_URL!,
|
|
57
|
+
identifierId: process.env.SMS_IDENTIFIER_ID!,
|
|
58
|
+
senderName: 'MyApp',
|
|
59
|
+
apiKey: process.env.SMS_API_KEY!,
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Integration
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { Hono } from 'hono';
|
|
69
|
+
|
|
70
|
+
const app = new Hono();
|
|
71
|
+
|
|
72
|
+
// Mount auth routes
|
|
73
|
+
app.route('/api/auth', auth.app);
|
|
74
|
+
|
|
75
|
+
// Or use as standalone handler
|
|
76
|
+
app.all('/api/auth/*', (c) => {
|
|
77
|
+
return auth.handler.fetch(c.req.raw);
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Access Session
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
app.use('*', async (c, next) => {
|
|
85
|
+
const session = await auth.getSession(c);
|
|
86
|
+
if (session.user) {
|
|
87
|
+
c.set('user', session.user);
|
|
88
|
+
}
|
|
89
|
+
await next();
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Access Documentation
|
|
94
|
+
|
|
95
|
+
After mounting routes, access:
|
|
96
|
+
- `/api/auth/docs` - Interactive Scalar UI
|
|
97
|
+
- `/api/auth/openapi.json` - OpenAPI specification
|
|
98
|
+
|
|
99
|
+
## Type-Safe RPC Client
|
|
100
|
+
|
|
101
|
+
Generate type-safe clients for your frontend:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { hc } from 'hono/client';
|
|
105
|
+
import type { MesobAuthApp } from '@mesob/auth-hono';
|
|
106
|
+
|
|
107
|
+
const authClient = hc<MesobAuthApp>('http://localhost:3000/api/auth', {
|
|
108
|
+
init: {
|
|
109
|
+
credentials: 'include', // Required for cookies
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// All API calls are fully type-safe
|
|
114
|
+
const user = await authClient.me.$get();
|
|
115
|
+
const result = await authClient['sign-in'].$post({
|
|
116
|
+
json: { identifier: 'user@example.com', password: 'password' },
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Documentation
|
|
121
|
+
|
|
122
|
+
- [Installation Guide](./docs/installation.md) - Step-by-step setup
|
|
123
|
+
- [Configuration](./docs/configuration.md) - All configuration options
|
|
124
|
+
- [Routes Reference](./docs/routes.md) - Complete API documentation
|
|
125
|
+
- [IAM System](./docs/iam.md) - IAM system overview
|
|
126
|
+
- [RPC Client](./docs/rpc-client.md) - Frontend client usage
|
|
127
|
+
|
|
128
|
+
## API Routes
|
|
129
|
+
|
|
130
|
+
### Authentication
|
|
131
|
+
- `POST /sign-up` - Sign up with email/phone
|
|
132
|
+
- `POST /sign-in` - Sign in
|
|
133
|
+
- `POST /sign-out` - Sign out
|
|
134
|
+
- `POST /check-user` - Check if user exists
|
|
135
|
+
|
|
136
|
+
### Profile
|
|
137
|
+
- `GET /me` - Get current user
|
|
138
|
+
- `GET /session` - Get current session
|
|
139
|
+
- `PUT /update` - Update profile
|
|
140
|
+
- `PUT /update-email` - Request email change
|
|
141
|
+
- `PUT /update-phone` - Request phone change
|
|
142
|
+
|
|
143
|
+
### Password
|
|
144
|
+
- `POST /password/forgot` - Request password reset
|
|
145
|
+
- `POST /password/reset` - Reset password
|
|
146
|
+
- `POST /password/verify` - Verify password
|
|
147
|
+
- `PUT /password/change` - Change password
|
|
148
|
+
|
|
149
|
+
### Email Verification
|
|
150
|
+
- `POST /email/verification/request` - Request verification code
|
|
151
|
+
- `POST /email/verification/confirm` - Confirm verification
|
|
152
|
+
|
|
153
|
+
### Phone Verification
|
|
154
|
+
- `POST /phone/verification/request` - Request OTP
|
|
155
|
+
- `POST /phone/verification/confirm` - Confirm OTP
|
|
156
|
+
|
|
157
|
+
### IAM Management
|
|
158
|
+
- `GET /users` - List users
|
|
159
|
+
- `GET /users/{id}` - Get user
|
|
160
|
+
- More IAM routes coming soon...
|
|
161
|
+
|
|
162
|
+
See [routes.md](./docs/routes.md) for complete API reference.
|
|
163
|
+
|
|
164
|
+
## Requirements
|
|
165
|
+
|
|
166
|
+
- Node.js 22+
|
|
167
|
+
- PostgreSQL database
|
|
168
|
+
- pnpm (recommended) or npm
|
|
169
|
+
|
|
170
|
+
## License
|
|
171
|
+
|
|
172
|
+
MIT
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import * as hono from 'hono';
|
|
2
|
+
import { OpenAPIHono } from '@hono/zod-openapi';
|
|
3
|
+
import { D as Database } from './index-ULpI-i0z.js';
|
|
4
|
+
|
|
5
|
+
type AuthEnv = {
|
|
6
|
+
Variables: {
|
|
7
|
+
config: AuthConfig;
|
|
8
|
+
database: Database;
|
|
9
|
+
tenantId: string;
|
|
10
|
+
userId?: string;
|
|
11
|
+
user?: User;
|
|
12
|
+
session?: Session;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
declare const createAuthRoutes: ({ config, database, }: CreateAuthRoutesOptions) => OpenAPIHono<AuthEnv, {}, "/">;
|
|
17
|
+
|
|
18
|
+
type UserRole = {
|
|
19
|
+
id: string;
|
|
20
|
+
roleId: string;
|
|
21
|
+
code: string;
|
|
22
|
+
name: unknown;
|
|
23
|
+
description: unknown;
|
|
24
|
+
};
|
|
25
|
+
type User = {
|
|
26
|
+
id: string;
|
|
27
|
+
tenantId: string;
|
|
28
|
+
fullName: string;
|
|
29
|
+
email: string | null;
|
|
30
|
+
phone: string | null;
|
|
31
|
+
handle: string;
|
|
32
|
+
image: string | null;
|
|
33
|
+
emailVerified: boolean;
|
|
34
|
+
phoneVerified: boolean;
|
|
35
|
+
lastSignInAt: string | null;
|
|
36
|
+
bannedUntil?: string | null;
|
|
37
|
+
loginAttempt?: number;
|
|
38
|
+
userRoles?: UserRole[];
|
|
39
|
+
};
|
|
40
|
+
type SessionMeta = {
|
|
41
|
+
action?: string;
|
|
42
|
+
rememberMe?: boolean;
|
|
43
|
+
};
|
|
44
|
+
type Session = {
|
|
45
|
+
id: string;
|
|
46
|
+
tenantId: string;
|
|
47
|
+
userId: string;
|
|
48
|
+
expiresAt: string;
|
|
49
|
+
createdAt: string;
|
|
50
|
+
updatedAt?: string;
|
|
51
|
+
userAgent: string | null;
|
|
52
|
+
ip: string | null;
|
|
53
|
+
meta?: SessionMeta | null;
|
|
54
|
+
};
|
|
55
|
+
type SendVerificationOTPParams = {
|
|
56
|
+
email?: string;
|
|
57
|
+
phone?: string;
|
|
58
|
+
code: string;
|
|
59
|
+
hash: string;
|
|
60
|
+
type: string;
|
|
61
|
+
};
|
|
62
|
+
type VerificationConfig = {
|
|
63
|
+
enabled: boolean;
|
|
64
|
+
required: boolean;
|
|
65
|
+
otpLength: number;
|
|
66
|
+
expiresIn: string;
|
|
67
|
+
maxAttempts: number;
|
|
68
|
+
resendInterval?: string;
|
|
69
|
+
sendVerificationOTP?: (params: SendVerificationOTPParams) => Promise<void> | void;
|
|
70
|
+
};
|
|
71
|
+
type EmailConfig = VerificationConfig;
|
|
72
|
+
type PhoneConfig = VerificationConfig;
|
|
73
|
+
type SessionConfig = {
|
|
74
|
+
/** Default session duration (e.g., '7d', '30d'). Default: '7d' */
|
|
75
|
+
expiresIn: string;
|
|
76
|
+
/** Session duration when "remember me" is checked (e.g., '30d'). Default: '30d' */
|
|
77
|
+
rememberMeExpiresIn: string;
|
|
78
|
+
/** Session duration when "remember me" is unchecked (e.g., '1h'). Default: '1h' */
|
|
79
|
+
shortSessionExpiresIn: string;
|
|
80
|
+
/** Refresh session if remaining time is less than this (e.g., '1d'). Default: '1d' */
|
|
81
|
+
updateAge: string;
|
|
82
|
+
/** Refresh short session if remaining time is less than this (e.g., '15m'). Default: '15m' */
|
|
83
|
+
shortSessionUpdateAge: string;
|
|
84
|
+
/** Maximum concurrent sessions per user */
|
|
85
|
+
maxPerUser?: number;
|
|
86
|
+
};
|
|
87
|
+
type SecurityConfig = {
|
|
88
|
+
maxLoginAttempts: number;
|
|
89
|
+
lockoutDuration: string;
|
|
90
|
+
};
|
|
91
|
+
type TenantConfig = {
|
|
92
|
+
enabled?: boolean;
|
|
93
|
+
tenantId?: string;
|
|
94
|
+
tenantDomains?: string[];
|
|
95
|
+
};
|
|
96
|
+
type DocsConfig = {
|
|
97
|
+
enabled?: boolean;
|
|
98
|
+
title?: string;
|
|
99
|
+
version?: string;
|
|
100
|
+
theme?: 'default' | 'moon' | 'purple' | 'saturn' | 'bluePlanet' | 'kepler' | 'mars';
|
|
101
|
+
servers?: Array<{
|
|
102
|
+
url: string;
|
|
103
|
+
description?: string;
|
|
104
|
+
}>;
|
|
105
|
+
};
|
|
106
|
+
type CookieConfig = {
|
|
107
|
+
prefix?: string;
|
|
108
|
+
};
|
|
109
|
+
type AuthConfig = {
|
|
110
|
+
connectionString: string;
|
|
111
|
+
secret: string;
|
|
112
|
+
basePath?: string;
|
|
113
|
+
tenant?: TenantConfig;
|
|
114
|
+
docs?: DocsConfig;
|
|
115
|
+
cookie?: CookieConfig;
|
|
116
|
+
session: SessionConfig;
|
|
117
|
+
email: EmailConfig;
|
|
118
|
+
phone: PhoneConfig;
|
|
119
|
+
security?: SecurityConfig;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
type CreateAuthRoutesOptions = {
|
|
123
|
+
config: AuthConfig;
|
|
124
|
+
database: Database;
|
|
125
|
+
};
|
|
126
|
+
type MesobAuth = {
|
|
127
|
+
routes: ReturnType<typeof createAuthRoutes>;
|
|
128
|
+
getSession: (c: hono.Context) => Promise<{
|
|
129
|
+
session: Session | null;
|
|
130
|
+
user: User | null;
|
|
131
|
+
sessionToken: string | null;
|
|
132
|
+
}>;
|
|
133
|
+
tenantMiddleware: hono.MiddlewareHandler;
|
|
134
|
+
sessionMiddleware: hono.MiddlewareHandler;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export type { AuthConfig as A, MesobAuth as M, SendVerificationOTPParams as S, User as U, Session as a, SessionConfig as b };
|