@morojs/moro 1.1.0 → 1.2.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 +56 -1
- package/dist/core/auth/morojs-adapter.d.ts +94 -0
- package/dist/core/auth/morojs-adapter.js +288 -0
- package/dist/core/auth/morojs-adapter.js.map +1 -0
- package/dist/core/http/http-server.d.ts +2 -0
- package/dist/core/http/http-server.js +52 -9
- package/dist/core/http/http-server.js.map +1 -1
- package/dist/core/middleware/built-in/auth-helpers.d.ts +124 -0
- package/dist/core/middleware/built-in/auth-helpers.js +338 -0
- package/dist/core/middleware/built-in/auth-helpers.js.map +1 -0
- package/dist/core/middleware/built-in/auth-providers.d.ts +125 -0
- package/dist/core/middleware/built-in/auth-providers.js +394 -0
- package/dist/core/middleware/built-in/auth-providers.js.map +1 -0
- package/dist/core/middleware/built-in/auth.d.ts +29 -1
- package/dist/core/middleware/built-in/auth.js +259 -16
- package/dist/core/middleware/built-in/auth.js.map +1 -1
- package/dist/core/middleware/built-in/index.d.ts +3 -1
- package/dist/core/middleware/built-in/index.js +19 -1
- package/dist/core/middleware/built-in/index.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/dist/moro.d.ts +1 -0
- package/dist/moro.js +19 -1
- package/dist/moro.js.map +1 -1
- package/dist/types/auth.d.ts +367 -0
- package/dist/types/auth.js +28 -0
- package/dist/types/auth.js.map +1 -0
- package/package.json +6 -2
- package/src/core/auth/README.md +339 -0
- package/src/core/auth/morojs-adapter.ts +402 -0
- package/src/core/http/http-server.ts +61 -10
- package/src/core/middleware/built-in/auth-helpers.ts +401 -0
- package/src/core/middleware/built-in/auth-providers.ts +480 -0
- package/src/core/middleware/built-in/auth.ts +306 -16
- package/src/core/middleware/built-in/index.ts +22 -0
- package/src/index.ts +26 -0
- package/src/moro.ts +29 -1
- package/src/types/auth.ts +440 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
# @auth/morojs - Auth.js Adapter for MoroJS
|
|
2
|
+
|
|
3
|
+
A native Auth.js adapter for the [MoroJS](https://github.com/MoroJS/moro) framework, providing seamless authentication integration without external dependencies.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This adapter allows Auth.js to work natively with MoroJS applications, providing:
|
|
8
|
+
|
|
9
|
+
- ✅ **Native MoroJS Integration** - Built specifically for MoroJS middleware patterns
|
|
10
|
+
- ✅ **Zero External Dependencies** - No reliance on Express adapters
|
|
11
|
+
- ✅ **Full Auth.js Compatibility** - Supports all Auth.js features and providers
|
|
12
|
+
- ✅ **Custom Transformers** - MoroJS-specific request/response handling
|
|
13
|
+
- ✅ **TypeScript First** - Complete type safety throughout
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @auth/core @auth/morojs
|
|
19
|
+
# or
|
|
20
|
+
pnpm add @auth/core @auth/morojs
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Basic Usage
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { Moro } from '@morojs/moro';
|
|
27
|
+
import { createAuthMiddleware } from '@auth/morojs';
|
|
28
|
+
import GitHub from '@auth/core/providers/github';
|
|
29
|
+
|
|
30
|
+
const app = new Moro();
|
|
31
|
+
|
|
32
|
+
app.use(
|
|
33
|
+
createAuthMiddleware({
|
|
34
|
+
providers: [
|
|
35
|
+
GitHub({
|
|
36
|
+
clientId: process.env.GITHUB_CLIENT_ID,
|
|
37
|
+
clientSecret: process.env.GITHUB_CLIENT_SECRET,
|
|
38
|
+
}),
|
|
39
|
+
],
|
|
40
|
+
secret: process.env.AUTH_SECRET,
|
|
41
|
+
})
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
app.get('/protected', (req, res) => {
|
|
45
|
+
if (!req.auth.isAuthenticated) {
|
|
46
|
+
return res.status(401).json({ error: 'Unauthorized' });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
res.json({ user: req.auth.user });
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
app.listen(3000);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Configuration
|
|
56
|
+
|
|
57
|
+
### Basic Configuration
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { createAuthMiddleware } from '@auth/morojs';
|
|
61
|
+
|
|
62
|
+
app.use(
|
|
63
|
+
createAuthMiddleware({
|
|
64
|
+
providers: [
|
|
65
|
+
// Your Auth.js providers
|
|
66
|
+
],
|
|
67
|
+
secret: process.env.AUTH_SECRET,
|
|
68
|
+
basePath: '/api/auth', // Default auth routes path
|
|
69
|
+
|
|
70
|
+
// Standard Auth.js options
|
|
71
|
+
session: {
|
|
72
|
+
strategy: 'jwt',
|
|
73
|
+
maxAge: 30 * 24 * 60 * 60, // 30 days
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
callbacks: {
|
|
77
|
+
// Auth.js callbacks
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
events: {
|
|
81
|
+
// Auth.js events
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### MoroJS-Specific Options
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
app.use(
|
|
91
|
+
createAuthMiddleware({
|
|
92
|
+
// ... standard Auth.js config
|
|
93
|
+
|
|
94
|
+
morojs: {
|
|
95
|
+
debug: true, // Enable MoroJS-specific logging
|
|
96
|
+
transformers: {
|
|
97
|
+
// Custom request transformer
|
|
98
|
+
request: req => {
|
|
99
|
+
// Transform MoroJS request for Auth.js
|
|
100
|
+
return req;
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
// Custom response transformer
|
|
104
|
+
response: res => {
|
|
105
|
+
// Transform Auth.js response for MoroJS
|
|
106
|
+
return res;
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
})
|
|
111
|
+
);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Request Object Extensions
|
|
115
|
+
|
|
116
|
+
The adapter automatically adds an `auth` object to the MoroJS request:
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
app.get('/api/user', (req, res) => {
|
|
120
|
+
// Auth status
|
|
121
|
+
const isAuthenticated = req.auth.isAuthenticated;
|
|
122
|
+
const user = req.auth.user;
|
|
123
|
+
const session = req.auth.session;
|
|
124
|
+
|
|
125
|
+
// Helper methods
|
|
126
|
+
const currentSession = await req.auth.getSession();
|
|
127
|
+
const currentUser = req.auth.getUser();
|
|
128
|
+
|
|
129
|
+
// Navigation helpers
|
|
130
|
+
const signInUrl = req.auth.signIn('github', { callbackUrl: '/dashboard' });
|
|
131
|
+
const signOutUrl = req.auth.signOut({ callbackUrl: '/' });
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Auth Routes
|
|
136
|
+
|
|
137
|
+
The adapter automatically handles standard Auth.js routes:
|
|
138
|
+
|
|
139
|
+
- `GET /api/auth/signin` - Sign in page
|
|
140
|
+
- `POST /api/auth/signin/:provider` - Sign in with provider
|
|
141
|
+
- `GET /api/auth/signout` - Sign out page
|
|
142
|
+
- `POST /api/auth/signout` - Sign out action
|
|
143
|
+
- `GET /api/auth/session` - Get current session
|
|
144
|
+
- `GET /api/auth/providers` - List available providers
|
|
145
|
+
- `GET /api/auth/csrf` - Get CSRF token
|
|
146
|
+
- `GET /api/auth/callback/:provider` - OAuth callbacks
|
|
147
|
+
|
|
148
|
+
## Advanced Usage
|
|
149
|
+
|
|
150
|
+
### Custom Provider Configuration
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
import { createAuthMiddleware } from '@auth/morojs';
|
|
154
|
+
|
|
155
|
+
app.use(
|
|
156
|
+
createAuthMiddleware({
|
|
157
|
+
providers: [
|
|
158
|
+
{
|
|
159
|
+
id: 'custom-oauth',
|
|
160
|
+
name: 'Custom OAuth',
|
|
161
|
+
type: 'oauth',
|
|
162
|
+
authorization: 'https://provider.com/oauth/authorize',
|
|
163
|
+
token: 'https://provider.com/oauth/token',
|
|
164
|
+
userinfo: 'https://provider.com/oauth/userinfo',
|
|
165
|
+
clientId: process.env.CUSTOM_CLIENT_ID,
|
|
166
|
+
clientSecret: process.env.CUSTOM_CLIENT_SECRET,
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
secret: process.env.AUTH_SECRET,
|
|
170
|
+
})
|
|
171
|
+
);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Session Management
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
app.get('/dashboard', async (req, res) => {
|
|
178
|
+
// Get current session
|
|
179
|
+
const session = await req.auth.getSession();
|
|
180
|
+
|
|
181
|
+
if (!session) {
|
|
182
|
+
const signInUrl = req.auth.signIn();
|
|
183
|
+
return res.redirect(signInUrl.url);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
res.json({
|
|
187
|
+
user: session.user,
|
|
188
|
+
expires: session.expires,
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Error Handling
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
app.use((error, req, res, next) => {
|
|
197
|
+
if (error.message?.includes('auth')) {
|
|
198
|
+
console.error('Auth error:', error);
|
|
199
|
+
return res.status(401).json({
|
|
200
|
+
error: 'Authentication error',
|
|
201
|
+
signInUrl: '/api/auth/signin',
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
next(error);
|
|
206
|
+
});
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## TypeScript Support
|
|
210
|
+
|
|
211
|
+
The adapter provides complete TypeScript support:
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
import type { MoroJSAuthConfig, MoroJSRequest, MoroJSResponse } from '@auth/morojs';
|
|
215
|
+
|
|
216
|
+
// Extend MoroJS request type
|
|
217
|
+
declare module '@morojs/moro' {
|
|
218
|
+
interface HttpRequest {
|
|
219
|
+
auth: {
|
|
220
|
+
isAuthenticated: boolean;
|
|
221
|
+
user: User | null;
|
|
222
|
+
session: Session | null;
|
|
223
|
+
getSession(): Promise<Session | null>;
|
|
224
|
+
getUser(): User | null;
|
|
225
|
+
signIn(provider?: string, options?: any): { url: string };
|
|
226
|
+
signOut(options?: any): { url: string };
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Environment Variables
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
# Required
|
|
236
|
+
AUTH_SECRET=your-auth-secret-here
|
|
237
|
+
|
|
238
|
+
# OAuth Providers (as needed)
|
|
239
|
+
GITHUB_CLIENT_ID=your-github-client-id
|
|
240
|
+
GITHUB_CLIENT_SECRET=your-github-client-secret
|
|
241
|
+
|
|
242
|
+
GOOGLE_CLIENT_ID=your-google-client-id
|
|
243
|
+
GOOGLE_CLIENT_SECRET=your-google-client-secret
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Migration from Other Frameworks
|
|
247
|
+
|
|
248
|
+
### From Express + NextAuth.js
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
// Before (Express + NextAuth.js)
|
|
252
|
+
import NextAuth from 'next-auth';
|
|
253
|
+
import { expressWrapper } from 'some-wrapper';
|
|
254
|
+
|
|
255
|
+
app.use('/api/auth/*', expressWrapper(NextAuth(config)));
|
|
256
|
+
|
|
257
|
+
// After (MoroJS + @auth/morojs)
|
|
258
|
+
import { createAuthMiddleware } from '@auth/morojs';
|
|
259
|
+
|
|
260
|
+
app.use(createAuthMiddleware(config));
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### From Other Auth Solutions
|
|
264
|
+
|
|
265
|
+
The adapter maintains Auth.js compatibility, so existing Auth.js configurations work with minimal changes.
|
|
266
|
+
|
|
267
|
+
## Examples
|
|
268
|
+
|
|
269
|
+
See the [examples directory](./examples/) for complete working examples:
|
|
270
|
+
|
|
271
|
+
- [Basic OAuth](./examples/basic-oauth.ts)
|
|
272
|
+
- [Multiple Providers](./examples/multiple-providers.ts)
|
|
273
|
+
- [Custom Callbacks](./examples/custom-callbacks.ts)
|
|
274
|
+
- [Database Sessions](./examples/database-sessions.ts)
|
|
275
|
+
|
|
276
|
+
## Contributing to Auth.js
|
|
277
|
+
|
|
278
|
+
This adapter is designed to be contributed to the Auth.js project as an official framework adapter.
|
|
279
|
+
|
|
280
|
+
### Contribution Steps
|
|
281
|
+
|
|
282
|
+
1. **Test thoroughly** with various Auth.js features
|
|
283
|
+
2. **Add comprehensive tests** covering all functionality
|
|
284
|
+
3. **Update documentation** to match Auth.js standards
|
|
285
|
+
4. **Submit PR** to [nextauthjs/next-auth](https://github.com/nextauthjs/next-auth)
|
|
286
|
+
5. **Follow Auth.js** contribution guidelines
|
|
287
|
+
|
|
288
|
+
### Package Structure for Auth.js
|
|
289
|
+
|
|
290
|
+
When contributing to Auth.js, the package structure should be:
|
|
291
|
+
|
|
292
|
+
```
|
|
293
|
+
packages/adapter-morojs/
|
|
294
|
+
├── src/
|
|
295
|
+
│ ├── index.ts # Main adapter export
|
|
296
|
+
│ ├── types.ts # TypeScript definitions
|
|
297
|
+
│ └── utils.ts # Helper utilities
|
|
298
|
+
├── tests/
|
|
299
|
+
│ ├── basic.test.ts # Basic functionality tests
|
|
300
|
+
│ ├── providers.test.ts # Provider-specific tests
|
|
301
|
+
│ └── edge-cases.test.ts # Edge case handling
|
|
302
|
+
├── package.json # Package configuration
|
|
303
|
+
├── README.md # This documentation
|
|
304
|
+
└── tsconfig.json # TypeScript configuration
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
## Comparison with Other Adapters
|
|
308
|
+
|
|
309
|
+
| Feature | @auth/express | @auth/morojs | Benefits |
|
|
310
|
+
| ---------------------- | ---------------- | -------------- | ------------------ |
|
|
311
|
+
| **Framework** | Express | MoroJS | Native integration |
|
|
312
|
+
| **Dependencies** | Express required | Zero external | Lighter bundle |
|
|
313
|
+
| **Request/Response** | Express objects | MoroJS objects | Better performance |
|
|
314
|
+
| **Middleware Pattern** | Express style | MoroJS hooks | More flexible |
|
|
315
|
+
| **TypeScript** | Good | Excellent | Better DX |
|
|
316
|
+
|
|
317
|
+
## Performance
|
|
318
|
+
|
|
319
|
+
The MoroJS adapter provides excellent performance characteristics:
|
|
320
|
+
|
|
321
|
+
- **Zero Express overhead** - Direct MoroJS integration
|
|
322
|
+
- **Efficient request handling** - Native object transformation
|
|
323
|
+
- **Optimized middleware** - Uses MoroJS hook system
|
|
324
|
+
- **Minimal memory footprint** - No unnecessary abstractions
|
|
325
|
+
|
|
326
|
+
## License
|
|
327
|
+
|
|
328
|
+
MIT - See [LICENSE](./LICENSE) file for details.
|
|
329
|
+
|
|
330
|
+
## Links
|
|
331
|
+
|
|
332
|
+
- [Auth.js Documentation](https://authjs.dev)
|
|
333
|
+
- [MoroJS Framework](https://github.com/MoroJS/moro)
|
|
334
|
+
- [Auth.js GitHub](https://github.com/nextauthjs/next-auth)
|
|
335
|
+
- [Report Issues](https://github.com/nextauthjs/next-auth/issues)
|
|
336
|
+
|
|
337
|
+
---
|
|
338
|
+
|
|
339
|
+
**Ready to contribute this adapter to Auth.js and get MoroJS recognized as an official framework! 🚀**
|