@t8n/iauth 1.0.0 → 1.0.1
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/LICENSE +15 -0
- package/README.md +558 -558
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Ezet Galaxy
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,558 +1,558 @@
|
|
|
1
|
-
# @t8n/iauth
|
|
2
|
-
|
|
3
|
-
Intelligent authentication extension for the **TitanPL framework**.
|
|
4
|
-
|
|
5
|
-
`@t8n/iauth` provides a **simple, synchronous authentication system** designed specifically for the **TitanPL Gravity Runtime**. It integrates password hashing, JWT authentication, OAuth login, and database-backed user management into a minimal API that works seamlessly with Titan actions.
|
|
6
|
-
|
|
7
|
-
The library follows Titan’s **sync-first architecture** and uses Titan native APIs wherever possible.
|
|
8
|
-
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
# Why This Exists
|
|
12
|
-
|
|
13
|
-
Authentication logic is one of the most repeated pieces of code in backend development.
|
|
14
|
-
|
|
15
|
-
In a typical Titan project, developers must manually implement:
|
|
16
|
-
|
|
17
|
-
* password hashing
|
|
18
|
-
* login validation
|
|
19
|
-
* JWT token generation
|
|
20
|
-
* token verification
|
|
21
|
-
* token extraction from request headers
|
|
22
|
-
* database user lookup
|
|
23
|
-
* protected route logic
|
|
24
|
-
* OAuth login flows
|
|
25
|
-
|
|
26
|
-
This results in duplicated authentication logic across multiple projects.
|
|
27
|
-
|
|
28
|
-
`@t8n/iauth` provides a **single reusable authentication layer** so developers can focus on building application logic instead of repeatedly rebuilding authentication systems.
|
|
29
|
-
|
|
30
|
-
---
|
|
31
|
-
|
|
32
|
-
# Features
|
|
33
|
-
|
|
34
|
-
## Authentication
|
|
35
|
-
|
|
36
|
-
* Password hashing using **bcryptjs**
|
|
37
|
-
* JWT token generation using **Titan native JWT**
|
|
38
|
-
* Token verification
|
|
39
|
-
* Automatic token extraction from request headers
|
|
40
|
-
* Protected route helper (`guard`)
|
|
41
|
-
|
|
42
|
-
## Database Integration
|
|
43
|
-
|
|
44
|
-
* Built-in user lookup
|
|
45
|
-
* Built-in user creation
|
|
46
|
-
* Configurable identity field
|
|
47
|
-
* Configurable password column
|
|
48
|
-
|
|
49
|
-
## OAuth Login
|
|
50
|
-
|
|
51
|
-
Supports OAuth login providers:
|
|
52
|
-
|
|
53
|
-
* Google
|
|
54
|
-
* GitHub
|
|
55
|
-
* Discord
|
|
56
|
-
|
|
57
|
-
Developers can integrate OAuth authentication in only a few lines of code.
|
|
58
|
-
|
|
59
|
-
## Titan Native Compatibility
|
|
60
|
-
|
|
61
|
-
The library is designed specifically for Titan:
|
|
62
|
-
|
|
63
|
-
* Titan JWT implementation
|
|
64
|
-
* Titan URL utilities
|
|
65
|
-
* Fully synchronous APIs
|
|
66
|
-
* Compatible with the Gravity runtime
|
|
67
|
-
|
|
68
|
-
---
|
|
69
|
-
|
|
70
|
-
# Installation
|
|
71
|
-
|
|
72
|
-
```bash
|
|
73
|
-
npm install @t8n/iauth
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
---
|
|
77
|
-
|
|
78
|
-
# Basic Setup
|
|
79
|
-
|
|
80
|
-
```javascript
|
|
81
|
-
import IAuth from "@t8n/iauth"
|
|
82
|
-
|
|
83
|
-
const auth = new IAuth({
|
|
84
|
-
secret: "supersecret",
|
|
85
|
-
|
|
86
|
-
db: {
|
|
87
|
-
conn: db,
|
|
88
|
-
table: "users"
|
|
89
|
-
}
|
|
90
|
-
})
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
---
|
|
94
|
-
|
|
95
|
-
# Configuration Options
|
|
96
|
-
|
|
97
|
-
| Option | Description |
|
|
98
|
-
| ------------------ | ------------------------------------- |
|
|
99
|
-
| `secret` | JWT signing secret |
|
|
100
|
-
| `exp` | Token expiration time |
|
|
101
|
-
| `db.conn` | Database connection |
|
|
102
|
-
| `db.table` | User table name |
|
|
103
|
-
| `db.identityField` | Identity column (default: `email`) |
|
|
104
|
-
| `db.passwordField` | Password column (default: `password`) |
|
|
105
|
-
| `beforeLogin` | Hook executed before login |
|
|
106
|
-
| `afterLogin` | Hook executed after login |
|
|
107
|
-
| `oauth` | OAuth provider configuration |
|
|
108
|
-
|
|
109
|
-
---
|
|
110
|
-
|
|
111
|
-
# Recommended Project Structure
|
|
112
|
-
|
|
113
|
-
For larger Titan applications, it is recommended to centralize authentication configuration in a dedicated folder.
|
|
114
|
-
|
|
115
|
-
```
|
|
116
|
-
app
|
|
117
|
-
├ actions
|
|
118
|
-
│ ├ login.js
|
|
119
|
-
│ ├ signup.js
|
|
120
|
-
│ └ profile.js
|
|
121
|
-
│
|
|
122
|
-
├ auth
|
|
123
|
-
│ └ config.js
|
|
124
|
-
│
|
|
125
|
-
└ app.js
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
This allows all actions to reuse the same authentication instance.
|
|
129
|
-
|
|
130
|
-
---
|
|
131
|
-
|
|
132
|
-
# Auth Configuration File
|
|
133
|
-
|
|
134
|
-
Create a shared configuration file.
|
|
135
|
-
|
|
136
|
-
```
|
|
137
|
-
app/auth/config.js
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
Example:
|
|
141
|
-
|
|
142
|
-
```javascript
|
|
143
|
-
import IAuth from "@t8n/iauth"
|
|
144
|
-
|
|
145
|
-
export const auth = new IAuth({
|
|
146
|
-
secret: "supersecret",
|
|
147
|
-
|
|
148
|
-
db: {
|
|
149
|
-
conn: db,
|
|
150
|
-
table: "users"
|
|
151
|
-
},
|
|
152
|
-
|
|
153
|
-
oauth: {
|
|
154
|
-
google: {
|
|
155
|
-
clientId: t.env.GOOGLE_CLIENT_ID,
|
|
156
|
-
clientSecret: t.env.GOOGLE_CLIENT_SECRET,
|
|
157
|
-
redirect: "http://localhost:5100/user"
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
})
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
Now the same authentication instance can be reused across all actions.
|
|
164
|
-
|
|
165
|
-
---
|
|
166
|
-
|
|
167
|
-
# Using Auth in Actions
|
|
168
|
-
|
|
169
|
-
Import the shared instance in any action.
|
|
170
|
-
|
|
171
|
-
Example:
|
|
172
|
-
|
|
173
|
-
```javascript
|
|
174
|
-
import { response } from "@titanpl/native"
|
|
175
|
-
import { auth } from "../auth/config"
|
|
176
|
-
|
|
177
|
-
export function profile(req) {
|
|
178
|
-
|
|
179
|
-
const user = auth.guard(req)
|
|
180
|
-
|
|
181
|
-
if (user.error) {
|
|
182
|
-
return response.json({ error: "Unauthorized" })
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return response.json({ user })
|
|
186
|
-
}
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
---
|
|
190
|
-
|
|
191
|
-
# Example Database Table
|
|
192
|
-
|
|
193
|
-
```sql
|
|
194
|
-
CREATE TABLE users (
|
|
195
|
-
id INT PRIMARY KEY AUTO_INCREMENT,
|
|
196
|
-
email VARCHAR(255) UNIQUE,
|
|
197
|
-
password TEXT
|
|
198
|
-
);
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
---
|
|
202
|
-
|
|
203
|
-
# Signup Example
|
|
204
|
-
|
|
205
|
-
```javascript
|
|
206
|
-
import { auth } from "../auth/config"
|
|
207
|
-
|
|
208
|
-
export function signup(req) {
|
|
209
|
-
|
|
210
|
-
const result = auth.signUp(req.body)
|
|
211
|
-
|
|
212
|
-
return result
|
|
213
|
-
}
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
Response:
|
|
217
|
-
|
|
218
|
-
```json
|
|
219
|
-
{
|
|
220
|
-
"user": {
|
|
221
|
-
"id": 1,
|
|
222
|
-
"email": "admin@test.com"
|
|
223
|
-
},
|
|
224
|
-
"token": "jwt-token"
|
|
225
|
-
}
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
---
|
|
229
|
-
|
|
230
|
-
# Login Example
|
|
231
|
-
|
|
232
|
-
```javascript
|
|
233
|
-
import { auth } from "../auth/config"
|
|
234
|
-
|
|
235
|
-
export function login(req) {
|
|
236
|
-
|
|
237
|
-
const result = auth.signIn(req.body)
|
|
238
|
-
|
|
239
|
-
return result
|
|
240
|
-
}
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
---
|
|
244
|
-
|
|
245
|
-
# Protected Route Example
|
|
246
|
-
|
|
247
|
-
```javascript
|
|
248
|
-
import { response } from "@titanpl/native"
|
|
249
|
-
import { auth } from "../auth/config"
|
|
250
|
-
|
|
251
|
-
export function profile(req) {
|
|
252
|
-
|
|
253
|
-
const user = auth.guard(req)
|
|
254
|
-
|
|
255
|
-
if (user.error) {
|
|
256
|
-
return response.json({ error: "Unauthorized" })
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
return response.json({ user })
|
|
260
|
-
}
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
The `guard()` helper automatically:
|
|
264
|
-
|
|
265
|
-
1. Extracts the JWT token from the request
|
|
266
|
-
2. Verifies the token
|
|
267
|
-
3. Returns the authenticated user
|
|
268
|
-
|
|
269
|
-
---
|
|
270
|
-
|
|
271
|
-
# OAuth Login
|
|
272
|
-
|
|
273
|
-
OAuth allows users to login using external providers such as Google, GitHub, or Discord.
|
|
274
|
-
|
|
275
|
-
---
|
|
276
|
-
|
|
277
|
-
# OAuth Configuration
|
|
278
|
-
|
|
279
|
-
```javascript
|
|
280
|
-
const auth = new IAuth({
|
|
281
|
-
secret: "supersecret",
|
|
282
|
-
|
|
283
|
-
oauth: {
|
|
284
|
-
google: {
|
|
285
|
-
clientId: "GOOGLE_CLIENT_ID",
|
|
286
|
-
clientSecret: "GOOGLE_CLIENT_SECRET",
|
|
287
|
-
redirect: "http://localhost:5100/user"
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
})
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
---
|
|
294
|
-
|
|
295
|
-
# OAuth Login Route
|
|
296
|
-
|
|
297
|
-
```javascript
|
|
298
|
-
import { response } from "@titanpl/native"
|
|
299
|
-
import { auth } from "../auth/config"
|
|
300
|
-
|
|
301
|
-
export function login() {
|
|
302
|
-
|
|
303
|
-
const google = auth.oauth("google")
|
|
304
|
-
|
|
305
|
-
return response.redirect(google.loginUrl())
|
|
306
|
-
|
|
307
|
-
}
|
|
308
|
-
```
|
|
309
|
-
|
|
310
|
-
Example route:
|
|
311
|
-
|
|
312
|
-
```
|
|
313
|
-
GET /lg
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
The user is redirected to the OAuth provider.
|
|
317
|
-
|
|
318
|
-
---
|
|
319
|
-
|
|
320
|
-
# OAuth Callback Example
|
|
321
|
-
|
|
322
|
-
```javascript
|
|
323
|
-
import { response } from "@titanpl/native"
|
|
324
|
-
import { auth } from "../auth/config"
|
|
325
|
-
|
|
326
|
-
export
|
|
327
|
-
|
|
328
|
-
const google = auth.oauth("google")
|
|
329
|
-
|
|
330
|
-
const { code } = req.query
|
|
331
|
-
|
|
332
|
-
const tokenData =
|
|
333
|
-
|
|
334
|
-
const profile =
|
|
335
|
-
|
|
336
|
-
const token = auth.signToken({
|
|
337
|
-
email: profile.email
|
|
338
|
-
})
|
|
339
|
-
|
|
340
|
-
return response.json({
|
|
341
|
-
message: "OAuth login successful",
|
|
342
|
-
token,
|
|
343
|
-
user: profile
|
|
344
|
-
})
|
|
345
|
-
}
|
|
346
|
-
```
|
|
347
|
-
|
|
348
|
-
---
|
|
349
|
-
|
|
350
|
-
# Route Configuration
|
|
351
|
-
|
|
352
|
-
OAuth routes must be defined manually in your Titan router.
|
|
353
|
-
|
|
354
|
-
Example `app.js`:
|
|
355
|
-
|
|
356
|
-
```javascript
|
|
357
|
-
import t from "@titanpl/route"
|
|
358
|
-
|
|
359
|
-
t.get("/lg").action("login")
|
|
360
|
-
t.get("/user").action("getuser")
|
|
361
|
-
|
|
362
|
-
t.get("/").reply("Ready to land on Titan Planet 🚀")
|
|
363
|
-
|
|
364
|
-
t.start(5100, "Titan Running!")
|
|
365
|
-
```
|
|
366
|
-
|
|
367
|
-
Routes:
|
|
368
|
-
|
|
369
|
-
```
|
|
370
|
-
GET /lg
|
|
371
|
-
```
|
|
372
|
-
|
|
373
|
-
Redirects the user to the OAuth provider.
|
|
374
|
-
|
|
375
|
-
```
|
|
376
|
-
GET /user
|
|
377
|
-
```
|
|
378
|
-
|
|
379
|
-
Handles the OAuth callback.
|
|
380
|
-
|
|
381
|
-
---
|
|
382
|
-
|
|
383
|
-
# OAuth Flow
|
|
384
|
-
|
|
385
|
-
```
|
|
386
|
-
GET /lg
|
|
387
|
-
↓
|
|
388
|
-
Redirect to Google
|
|
389
|
-
↓
|
|
390
|
-
User logs in
|
|
391
|
-
↓
|
|
392
|
-
Google redirects back
|
|
393
|
-
↓
|
|
394
|
-
GET /user?code=xxxxx
|
|
395
|
-
↓
|
|
396
|
-
Code exchanged for access token
|
|
397
|
-
↓
|
|
398
|
-
User profile retrieved
|
|
399
|
-
↓
|
|
400
|
-
JWT session created
|
|
401
|
-
```
|
|
402
|
-
|
|
403
|
-
---
|
|
404
|
-
|
|
405
|
-
# JWT Token Structure
|
|
406
|
-
|
|
407
|
-
```json
|
|
408
|
-
{
|
|
409
|
-
"id": 1,
|
|
410
|
-
"email": "admin@test.com",
|
|
411
|
-
"exp": 1773222105,
|
|
412
|
-
"exp_readable": "2026-03-11T09:41:45.000Z"
|
|
413
|
-
}
|
|
414
|
-
```
|
|
415
|
-
|
|
416
|
-
`exp_readable` is automatically added for easier debugging.
|
|
417
|
-
|
|
418
|
-
---
|
|
419
|
-
|
|
420
|
-
# Internal Authentication Flow
|
|
421
|
-
|
|
422
|
-
## Signup
|
|
423
|
-
|
|
424
|
-
```
|
|
425
|
-
user submits email/password
|
|
426
|
-
↓
|
|
427
|
-
password hashed using bcrypt
|
|
428
|
-
↓
|
|
429
|
-
user stored in database
|
|
430
|
-
↓
|
|
431
|
-
JWT token generated
|
|
432
|
-
↓
|
|
433
|
-
user + token returned
|
|
434
|
-
```
|
|
435
|
-
|
|
436
|
-
## Login
|
|
437
|
-
|
|
438
|
-
```
|
|
439
|
-
email lookup in database
|
|
440
|
-
↓
|
|
441
|
-
password verified using bcrypt
|
|
442
|
-
↓
|
|
443
|
-
JWT token created
|
|
444
|
-
↓
|
|
445
|
-
token returned
|
|
446
|
-
```
|
|
447
|
-
|
|
448
|
-
## Protected Route
|
|
449
|
-
|
|
450
|
-
```
|
|
451
|
-
request received
|
|
452
|
-
↓
|
|
453
|
-
Authorization header parsed
|
|
454
|
-
↓
|
|
455
|
-
JWT verified
|
|
456
|
-
↓
|
|
457
|
-
user returned
|
|
458
|
-
```
|
|
459
|
-
|
|
460
|
-
---
|
|
461
|
-
|
|
462
|
-
# API Reference
|
|
463
|
-
|
|
464
|
-
## Password Utilities
|
|
465
|
-
|
|
466
|
-
```javascript
|
|
467
|
-
auth.hashPassword(password)
|
|
468
|
-
auth.verifyPassword(password, hash)
|
|
469
|
-
```
|
|
470
|
-
|
|
471
|
-
## JWT
|
|
472
|
-
|
|
473
|
-
```javascript
|
|
474
|
-
auth.signToken(payload)
|
|
475
|
-
auth.verifyToken(token)
|
|
476
|
-
```
|
|
477
|
-
|
|
478
|
-
## Token Utilities
|
|
479
|
-
|
|
480
|
-
```javascript
|
|
481
|
-
auth.extractToken(req)
|
|
482
|
-
auth.getUser(req)
|
|
483
|
-
auth.guard(req)
|
|
484
|
-
```
|
|
485
|
-
|
|
486
|
-
## Authentication
|
|
487
|
-
|
|
488
|
-
```javascript
|
|
489
|
-
auth.signUp(data)
|
|
490
|
-
auth.signIn(data)
|
|
491
|
-
```
|
|
492
|
-
|
|
493
|
-
## OAuth
|
|
494
|
-
|
|
495
|
-
```javascript
|
|
496
|
-
auth.oauth("google")
|
|
497
|
-
auth.oauth("github")
|
|
498
|
-
auth.oauth("discord")
|
|
499
|
-
```
|
|
500
|
-
|
|
501
|
-
---
|
|
502
|
-
|
|
503
|
-
# Why This Makes Titan Development Easier
|
|
504
|
-
|
|
505
|
-
Without this extension, Titan developers would need to manually implement:
|
|
506
|
-
|
|
507
|
-
* password hashing
|
|
508
|
-
* JWT creation
|
|
509
|
-
* token validation
|
|
510
|
-
* database queries
|
|
511
|
-
* token parsing
|
|
512
|
-
* protected route checks
|
|
513
|
-
|
|
514
|
-
This logic would appear in **every project**.
|
|
515
|
-
|
|
516
|
-
With `@t8n/iauth`, authentication becomes:
|
|
517
|
-
|
|
518
|
-
```javascript
|
|
519
|
-
auth.signUp()
|
|
520
|
-
auth.signIn()
|
|
521
|
-
auth.guard()
|
|
522
|
-
```
|
|
523
|
-
|
|
524
|
-
This reduces boilerplate and keeps authentication consistent across Titan projects.
|
|
525
|
-
|
|
526
|
-
---
|
|
527
|
-
|
|
528
|
-
# Design Philosophy
|
|
529
|
-
|
|
530
|
-
The library follows Titan design principles:
|
|
531
|
-
|
|
532
|
-
* synchronous APIs
|
|
533
|
-
* minimal abstractions
|
|
534
|
-
* native Titan compatibility
|
|
535
|
-
* developer-controlled database
|
|
536
|
-
* lightweight architecture
|
|
537
|
-
|
|
538
|
-
`@t8n/iauth` is not a heavy framework. It is a **thin authentication layer designed specifically for Titan**.
|
|
539
|
-
|
|
540
|
-
---
|
|
541
|
-
|
|
542
|
-
# Future Features
|
|
543
|
-
|
|
544
|
-
Planned improvements include:
|
|
545
|
-
|
|
546
|
-
* Role-based authorization
|
|
547
|
-
* Refresh tokens
|
|
548
|
-
* Session-based authentication
|
|
549
|
-
* Password reset flows
|
|
550
|
-
* Email verification
|
|
551
|
-
* Login rate limiting
|
|
552
|
-
* OAuth auto-routing
|
|
553
|
-
|
|
554
|
-
---
|
|
555
|
-
|
|
556
|
-
# License
|
|
557
|
-
|
|
558
|
-
ISC
|
|
1
|
+
# @t8n/iauth
|
|
2
|
+
|
|
3
|
+
Intelligent authentication extension for the **TitanPL framework**.
|
|
4
|
+
|
|
5
|
+
`@t8n/iauth` provides a **simple, synchronous authentication system** designed specifically for the **TitanPL Gravity Runtime**. It integrates password hashing, JWT authentication, OAuth login, and database-backed user management into a minimal API that works seamlessly with Titan actions.
|
|
6
|
+
|
|
7
|
+
The library follows Titan’s **sync-first architecture** and uses Titan native APIs wherever possible.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# Why This Exists
|
|
12
|
+
|
|
13
|
+
Authentication logic is one of the most repeated pieces of code in backend development.
|
|
14
|
+
|
|
15
|
+
In a typical Titan project, developers must manually implement:
|
|
16
|
+
|
|
17
|
+
* password hashing
|
|
18
|
+
* login validation
|
|
19
|
+
* JWT token generation
|
|
20
|
+
* token verification
|
|
21
|
+
* token extraction from request headers
|
|
22
|
+
* database user lookup
|
|
23
|
+
* protected route logic
|
|
24
|
+
* OAuth login flows
|
|
25
|
+
|
|
26
|
+
This results in duplicated authentication logic across multiple projects.
|
|
27
|
+
|
|
28
|
+
`@t8n/iauth` provides a **single reusable authentication layer** so developers can focus on building application logic instead of repeatedly rebuilding authentication systems.
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
# Features
|
|
33
|
+
|
|
34
|
+
## Authentication
|
|
35
|
+
|
|
36
|
+
* Password hashing using **bcryptjs**
|
|
37
|
+
* JWT token generation using **Titan native JWT**
|
|
38
|
+
* Token verification
|
|
39
|
+
* Automatic token extraction from request headers
|
|
40
|
+
* Protected route helper (`guard`)
|
|
41
|
+
|
|
42
|
+
## Database Integration
|
|
43
|
+
|
|
44
|
+
* Built-in user lookup
|
|
45
|
+
* Built-in user creation
|
|
46
|
+
* Configurable identity field
|
|
47
|
+
* Configurable password column
|
|
48
|
+
|
|
49
|
+
## OAuth Login
|
|
50
|
+
|
|
51
|
+
Supports OAuth login providers:
|
|
52
|
+
|
|
53
|
+
* Google
|
|
54
|
+
* GitHub
|
|
55
|
+
* Discord
|
|
56
|
+
|
|
57
|
+
Developers can integrate OAuth authentication in only a few lines of code.
|
|
58
|
+
|
|
59
|
+
## Titan Native Compatibility
|
|
60
|
+
|
|
61
|
+
The library is designed specifically for Titan:
|
|
62
|
+
|
|
63
|
+
* Titan JWT implementation
|
|
64
|
+
* Titan URL utilities
|
|
65
|
+
* Fully synchronous APIs
|
|
66
|
+
* Compatible with the Gravity runtime
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
# Installation
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
npm install @t8n/iauth
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
# Basic Setup
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
import IAuth from "@t8n/iauth"
|
|
82
|
+
|
|
83
|
+
const auth = new IAuth({
|
|
84
|
+
secret: "supersecret",
|
|
85
|
+
|
|
86
|
+
db: {
|
|
87
|
+
conn: db,
|
|
88
|
+
table: "users"
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
# Configuration Options
|
|
96
|
+
|
|
97
|
+
| Option | Description |
|
|
98
|
+
| ------------------ | ------------------------------------- |
|
|
99
|
+
| `secret` | JWT signing secret |
|
|
100
|
+
| `exp` | Token expiration time |
|
|
101
|
+
| `db.conn` | Database connection |
|
|
102
|
+
| `db.table` | User table name |
|
|
103
|
+
| `db.identityField` | Identity column (default: `email`) |
|
|
104
|
+
| `db.passwordField` | Password column (default: `password`) |
|
|
105
|
+
| `beforeLogin` | Hook executed before login |
|
|
106
|
+
| `afterLogin` | Hook executed after login |
|
|
107
|
+
| `oauth` | OAuth provider configuration |
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
# Recommended Project Structure
|
|
112
|
+
|
|
113
|
+
For larger Titan applications, it is recommended to centralize authentication configuration in a dedicated folder.
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
app
|
|
117
|
+
├ actions
|
|
118
|
+
│ ├ login.js
|
|
119
|
+
│ ├ signup.js
|
|
120
|
+
│ └ profile.js
|
|
121
|
+
│
|
|
122
|
+
├ auth
|
|
123
|
+
│ └ config.js
|
|
124
|
+
│
|
|
125
|
+
└ app.js
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
This allows all actions to reuse the same authentication instance.
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
# Auth Configuration File
|
|
133
|
+
|
|
134
|
+
Create a shared configuration file.
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
app/auth/config.js
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Example:
|
|
141
|
+
|
|
142
|
+
```javascript
|
|
143
|
+
import IAuth from "@t8n/iauth"
|
|
144
|
+
|
|
145
|
+
export const auth = new IAuth({
|
|
146
|
+
secret: "supersecret",
|
|
147
|
+
|
|
148
|
+
db: {
|
|
149
|
+
conn: db,
|
|
150
|
+
table: "users"
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
oauth: {
|
|
154
|
+
google: {
|
|
155
|
+
clientId: t.env.GOOGLE_CLIENT_ID,
|
|
156
|
+
clientSecret: t.env.GOOGLE_CLIENT_SECRET,
|
|
157
|
+
redirect: "http://localhost:5100/user"
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Now the same authentication instance can be reused across all actions.
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
# Using Auth in Actions
|
|
168
|
+
|
|
169
|
+
Import the shared instance in any action.
|
|
170
|
+
|
|
171
|
+
Example:
|
|
172
|
+
|
|
173
|
+
```javascript
|
|
174
|
+
import { response } from "@titanpl/native"
|
|
175
|
+
import { auth } from "../auth/config"
|
|
176
|
+
|
|
177
|
+
export function profile(req) {
|
|
178
|
+
|
|
179
|
+
const user = auth.guard(req)
|
|
180
|
+
|
|
181
|
+
if (user.error) {
|
|
182
|
+
return response.json({ error: "Unauthorized" })
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return response.json({ user })
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
# Example Database Table
|
|
192
|
+
|
|
193
|
+
```sql
|
|
194
|
+
CREATE TABLE users (
|
|
195
|
+
id INT PRIMARY KEY AUTO_INCREMENT,
|
|
196
|
+
email VARCHAR(255) UNIQUE,
|
|
197
|
+
password TEXT
|
|
198
|
+
);
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
# Signup Example
|
|
204
|
+
|
|
205
|
+
```javascript
|
|
206
|
+
import { auth } from "../auth/config"
|
|
207
|
+
|
|
208
|
+
export function signup(req) {
|
|
209
|
+
|
|
210
|
+
const result = auth.signUp(req.body)
|
|
211
|
+
|
|
212
|
+
return result
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Response:
|
|
217
|
+
|
|
218
|
+
```json
|
|
219
|
+
{
|
|
220
|
+
"user": {
|
|
221
|
+
"id": 1,
|
|
222
|
+
"email": "admin@test.com"
|
|
223
|
+
},
|
|
224
|
+
"token": "jwt-token"
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
# Login Example
|
|
231
|
+
|
|
232
|
+
```javascript
|
|
233
|
+
import { auth } from "../auth/config"
|
|
234
|
+
|
|
235
|
+
export function login(req) {
|
|
236
|
+
|
|
237
|
+
const result = auth.signIn(req.body)
|
|
238
|
+
|
|
239
|
+
return result
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
---
|
|
244
|
+
|
|
245
|
+
# Protected Route Example
|
|
246
|
+
|
|
247
|
+
```javascript
|
|
248
|
+
import { response } from "@titanpl/native"
|
|
249
|
+
import { auth } from "../auth/config"
|
|
250
|
+
|
|
251
|
+
export function profile(req) {
|
|
252
|
+
|
|
253
|
+
const user = auth.guard(req)
|
|
254
|
+
|
|
255
|
+
if (user.error) {
|
|
256
|
+
return response.json({ error: "Unauthorized" })
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return response.json({ user })
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
The `guard()` helper automatically:
|
|
264
|
+
|
|
265
|
+
1. Extracts the JWT token from the request
|
|
266
|
+
2. Verifies the token
|
|
267
|
+
3. Returns the authenticated user
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
# OAuth Login
|
|
272
|
+
|
|
273
|
+
OAuth allows users to login using external providers such as Google, GitHub, or Discord.
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
|
|
277
|
+
# OAuth Configuration
|
|
278
|
+
|
|
279
|
+
```javascript
|
|
280
|
+
const auth = new IAuth({
|
|
281
|
+
secret: "supersecret",
|
|
282
|
+
|
|
283
|
+
oauth: {
|
|
284
|
+
google: {
|
|
285
|
+
clientId: "GOOGLE_CLIENT_ID",
|
|
286
|
+
clientSecret: "GOOGLE_CLIENT_SECRET",
|
|
287
|
+
redirect: "http://localhost:5100/user"
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
})
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
---
|
|
294
|
+
|
|
295
|
+
# OAuth Login Route
|
|
296
|
+
|
|
297
|
+
```javascript
|
|
298
|
+
import { response } from "@titanpl/native"
|
|
299
|
+
import { auth } from "../auth/config"
|
|
300
|
+
|
|
301
|
+
export function login() {
|
|
302
|
+
|
|
303
|
+
const google = auth.oauth("google")
|
|
304
|
+
|
|
305
|
+
return response.redirect(google.loginUrl())
|
|
306
|
+
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
Example route:
|
|
311
|
+
|
|
312
|
+
```
|
|
313
|
+
GET /lg
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
The user is redirected to the OAuth provider.
|
|
317
|
+
|
|
318
|
+
---
|
|
319
|
+
|
|
320
|
+
# OAuth Callback Example
|
|
321
|
+
|
|
322
|
+
```javascript
|
|
323
|
+
import { response } from "@titanpl/native"
|
|
324
|
+
import { auth } from "../auth/config"
|
|
325
|
+
|
|
326
|
+
export function getuser(req) {
|
|
327
|
+
|
|
328
|
+
const google = auth.oauth("google")
|
|
329
|
+
|
|
330
|
+
const { code } = req.query
|
|
331
|
+
|
|
332
|
+
const tokenData = google.exchange(code)
|
|
333
|
+
|
|
334
|
+
const profile = google.profile(tokenData.access_token)
|
|
335
|
+
|
|
336
|
+
const token = auth.signToken({
|
|
337
|
+
email: profile.email
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
return response.json({
|
|
341
|
+
message: "OAuth login successful",
|
|
342
|
+
token,
|
|
343
|
+
user: profile
|
|
344
|
+
})
|
|
345
|
+
}
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
---
|
|
349
|
+
|
|
350
|
+
# Route Configuration
|
|
351
|
+
|
|
352
|
+
OAuth routes must be defined manually in your Titan router.
|
|
353
|
+
|
|
354
|
+
Example `app.js`:
|
|
355
|
+
|
|
356
|
+
```javascript
|
|
357
|
+
import t from "@titanpl/route"
|
|
358
|
+
|
|
359
|
+
t.get("/lg").action("login")
|
|
360
|
+
t.get("/user").action("getuser")
|
|
361
|
+
|
|
362
|
+
t.get("/").reply("Ready to land on Titan Planet 🚀")
|
|
363
|
+
|
|
364
|
+
t.start(5100, "Titan Running!")
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
Routes:
|
|
368
|
+
|
|
369
|
+
```
|
|
370
|
+
GET /lg
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
Redirects the user to the OAuth provider.
|
|
374
|
+
|
|
375
|
+
```
|
|
376
|
+
GET /user
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
Handles the OAuth callback.
|
|
380
|
+
|
|
381
|
+
---
|
|
382
|
+
|
|
383
|
+
# OAuth Flow
|
|
384
|
+
|
|
385
|
+
```
|
|
386
|
+
GET /lg
|
|
387
|
+
↓
|
|
388
|
+
Redirect to Google
|
|
389
|
+
↓
|
|
390
|
+
User logs in
|
|
391
|
+
↓
|
|
392
|
+
Google redirects back
|
|
393
|
+
↓
|
|
394
|
+
GET /user?code=xxxxx
|
|
395
|
+
↓
|
|
396
|
+
Code exchanged for access token
|
|
397
|
+
↓
|
|
398
|
+
User profile retrieved
|
|
399
|
+
↓
|
|
400
|
+
JWT session created
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
---
|
|
404
|
+
|
|
405
|
+
# JWT Token Structure
|
|
406
|
+
|
|
407
|
+
```json
|
|
408
|
+
{
|
|
409
|
+
"id": 1,
|
|
410
|
+
"email": "admin@test.com",
|
|
411
|
+
"exp": 1773222105,
|
|
412
|
+
"exp_readable": "2026-03-11T09:41:45.000Z"
|
|
413
|
+
}
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
`exp_readable` is automatically added for easier debugging.
|
|
417
|
+
|
|
418
|
+
---
|
|
419
|
+
|
|
420
|
+
# Internal Authentication Flow
|
|
421
|
+
|
|
422
|
+
## Signup
|
|
423
|
+
|
|
424
|
+
```
|
|
425
|
+
user submits email/password
|
|
426
|
+
↓
|
|
427
|
+
password hashed using bcrypt
|
|
428
|
+
↓
|
|
429
|
+
user stored in database
|
|
430
|
+
↓
|
|
431
|
+
JWT token generated
|
|
432
|
+
↓
|
|
433
|
+
user + token returned
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
## Login
|
|
437
|
+
|
|
438
|
+
```
|
|
439
|
+
email lookup in database
|
|
440
|
+
↓
|
|
441
|
+
password verified using bcrypt
|
|
442
|
+
↓
|
|
443
|
+
JWT token created
|
|
444
|
+
↓
|
|
445
|
+
token returned
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
## Protected Route
|
|
449
|
+
|
|
450
|
+
```
|
|
451
|
+
request received
|
|
452
|
+
↓
|
|
453
|
+
Authorization header parsed
|
|
454
|
+
↓
|
|
455
|
+
JWT verified
|
|
456
|
+
↓
|
|
457
|
+
user returned
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
---
|
|
461
|
+
|
|
462
|
+
# API Reference
|
|
463
|
+
|
|
464
|
+
## Password Utilities
|
|
465
|
+
|
|
466
|
+
```javascript
|
|
467
|
+
auth.hashPassword(password)
|
|
468
|
+
auth.verifyPassword(password, hash)
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
## JWT
|
|
472
|
+
|
|
473
|
+
```javascript
|
|
474
|
+
auth.signToken(payload)
|
|
475
|
+
auth.verifyToken(token)
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
## Token Utilities
|
|
479
|
+
|
|
480
|
+
```javascript
|
|
481
|
+
auth.extractToken(req)
|
|
482
|
+
auth.getUser(req)
|
|
483
|
+
auth.guard(req)
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
## Authentication
|
|
487
|
+
|
|
488
|
+
```javascript
|
|
489
|
+
auth.signUp(data)
|
|
490
|
+
auth.signIn(data)
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
## OAuth
|
|
494
|
+
|
|
495
|
+
```javascript
|
|
496
|
+
auth.oauth("google")
|
|
497
|
+
auth.oauth("github")
|
|
498
|
+
auth.oauth("discord")
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
---
|
|
502
|
+
|
|
503
|
+
# Why This Makes Titan Development Easier
|
|
504
|
+
|
|
505
|
+
Without this extension, Titan developers would need to manually implement:
|
|
506
|
+
|
|
507
|
+
* password hashing
|
|
508
|
+
* JWT creation
|
|
509
|
+
* token validation
|
|
510
|
+
* database queries
|
|
511
|
+
* token parsing
|
|
512
|
+
* protected route checks
|
|
513
|
+
|
|
514
|
+
This logic would appear in **every project**.
|
|
515
|
+
|
|
516
|
+
With `@t8n/iauth`, authentication becomes:
|
|
517
|
+
|
|
518
|
+
```javascript
|
|
519
|
+
auth.signUp()
|
|
520
|
+
auth.signIn()
|
|
521
|
+
auth.guard()
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
This reduces boilerplate and keeps authentication consistent across Titan projects.
|
|
525
|
+
|
|
526
|
+
---
|
|
527
|
+
|
|
528
|
+
# Design Philosophy
|
|
529
|
+
|
|
530
|
+
The library follows Titan design principles:
|
|
531
|
+
|
|
532
|
+
* synchronous APIs
|
|
533
|
+
* minimal abstractions
|
|
534
|
+
* native Titan compatibility
|
|
535
|
+
* developer-controlled database
|
|
536
|
+
* lightweight architecture
|
|
537
|
+
|
|
538
|
+
`@t8n/iauth` is not a heavy framework. It is a **thin authentication layer designed specifically for Titan**.
|
|
539
|
+
|
|
540
|
+
---
|
|
541
|
+
|
|
542
|
+
# Future Features
|
|
543
|
+
|
|
544
|
+
Planned improvements include:
|
|
545
|
+
|
|
546
|
+
* Role-based authorization
|
|
547
|
+
* Refresh tokens
|
|
548
|
+
* Session-based authentication
|
|
549
|
+
* Password reset flows
|
|
550
|
+
* Email verification
|
|
551
|
+
* Login rate limiting
|
|
552
|
+
* OAuth auto-routing
|
|
553
|
+
|
|
554
|
+
---
|
|
555
|
+
|
|
556
|
+
# License
|
|
557
|
+
|
|
558
|
+
ISC
|