@meridianjs/auth 0.1.7 → 0.1.8
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 +131 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# @meridianjs/auth
|
|
2
|
+
|
|
3
|
+
Authentication module for MeridianJS. Provides JWT-based register/login flows, Google OAuth support, Express middleware for token verification, and RBAC guards.
|
|
4
|
+
|
|
5
|
+
Auto-loaded by `@meridianjs/meridian` — you do not need to add this to `modules[]` yourself.
|
|
6
|
+
|
|
7
|
+
## Service: `authModuleService`
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
const svc = req.scope.resolve("authModuleService") as any
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Methods
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
// Register a new user — returns { user, token }
|
|
17
|
+
const { user, token } = await svc.register({
|
|
18
|
+
email: "alice@example.com",
|
|
19
|
+
password: "password123",
|
|
20
|
+
first_name: "Alice",
|
|
21
|
+
last_name: "Smith",
|
|
22
|
+
role: "member", // optional: "super-admin" | "admin" | "moderator" | "member"
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
// Login — returns { user, token }
|
|
26
|
+
const { user, token } = await svc.login({
|
|
27
|
+
email: "alice@example.com",
|
|
28
|
+
password: "password123",
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
// Verify and decode a JWT
|
|
32
|
+
const payload = await svc.verifyToken(token)
|
|
33
|
+
// payload: { sub, workspaceId, roles, permissions, jti }
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Middleware
|
|
37
|
+
|
|
38
|
+
### `authenticateJWT`
|
|
39
|
+
|
|
40
|
+
Verifies the `Authorization: Bearer <token>` header and attaches `req.user`. Returns `401` if the token is missing or invalid.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { authenticateJWT } from "@meridianjs/auth"
|
|
44
|
+
|
|
45
|
+
// Applied globally in middlewares.ts:
|
|
46
|
+
export default {
|
|
47
|
+
routes: [
|
|
48
|
+
{ matcher: "/admin", middlewares: [authenticateJWT] },
|
|
49
|
+
],
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Or inline in a route:
|
|
53
|
+
export const GET = [authenticateJWT, async (req: any, res: Response) => {
|
|
54
|
+
res.json({ userId: req.user.id })
|
|
55
|
+
}]
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### `requireRoles`
|
|
59
|
+
|
|
60
|
+
Guard a route or handler to specific roles. Returns `403` if the user's roles don't match.
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { requireRoles } from "@meridianjs/auth"
|
|
64
|
+
|
|
65
|
+
export const DELETE = [
|
|
66
|
+
authenticateJWT,
|
|
67
|
+
requireRoles("admin", "super-admin"),
|
|
68
|
+
async (req: any, res: Response) => {
|
|
69
|
+
// Only admins reach here
|
|
70
|
+
},
|
|
71
|
+
]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### `requirePermission`
|
|
75
|
+
|
|
76
|
+
Guard based on a specific permission string from a custom AppRole:
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { requirePermission } from "@meridianjs/auth"
|
|
80
|
+
|
|
81
|
+
export const POST = [
|
|
82
|
+
authenticateJWT,
|
|
83
|
+
requirePermission("issues:create"),
|
|
84
|
+
async (req: any, res: Response) => { /* ... */ },
|
|
85
|
+
]
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## JWT Payload
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
interface JwtPayload {
|
|
92
|
+
sub: string // User ID
|
|
93
|
+
workspaceId: string // Active workspace ID (null until workspace selected)
|
|
94
|
+
roles: string[] // e.g. ["admin"] or ["member"]
|
|
95
|
+
permissions: string[] // Custom AppRole permissions (e.g. ["issues:create", "issues:delete"])
|
|
96
|
+
jti: string // Unique token ID (for revocation)
|
|
97
|
+
iat: number
|
|
98
|
+
exp: number
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Tokens expire after **7 days** by default. Configure in `projectConfig`:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
projectConfig: {
|
|
106
|
+
jwtSecret: process.env.JWT_SECRET!,
|
|
107
|
+
jwtExpiresIn: "30d", // optional
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Google OAuth
|
|
112
|
+
|
|
113
|
+
When `@meridianjs/google-oauth` is configured, the auth module exposes:
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
GET /auth/google → Redirects to Google consent screen
|
|
117
|
+
GET /auth/google/callback → Handles OAuth code, returns JWT
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## API Routes
|
|
121
|
+
|
|
122
|
+
| Method | Path | Description |
|
|
123
|
+
|---|---|---|
|
|
124
|
+
| `POST` | `/auth/register` | Register, return `{ user, token }` |
|
|
125
|
+
| `POST` | `/auth/login` | Login, return `{ user, token }` |
|
|
126
|
+
| `GET` | `/auth/invite/:token` | Validate an invitation token |
|
|
127
|
+
| `POST` | `/auth/invite/:token` | Accept invitation (register or login) |
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
MIT
|