@cauth/express 0.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/README.md +140 -0
- package/dist/index.cjs +28 -0
- package/dist/index.d.cts +341 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# @cauth/express
|
|
2
|
+
|
|
3
|
+
Express integration for CAuth authentication system.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Express Integration**: Seamless integration with Express.js applications
|
|
8
|
+
- **Type-Safe Routes**: TypeScript support for route handlers
|
|
9
|
+
- **Authentication Middleware**: Ready-to-use authentication guard
|
|
10
|
+
- **Request Augmentation**: Typed user data in request object
|
|
11
|
+
- **Error Handling**: Express-compatible error handling
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @cauth/express @cauth/core
|
|
17
|
+
# or
|
|
18
|
+
yarn add @cauth/express @cauth/core
|
|
19
|
+
# or
|
|
20
|
+
pnpm add @cauth/express @cauth/core
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import express from 'express';
|
|
27
|
+
import { CAuth } from '@cauth/core';
|
|
28
|
+
import { ExpressContractor, Guard } from '@cauth/express';
|
|
29
|
+
import { PrismaContractor } from '@cauth/prisma';
|
|
30
|
+
|
|
31
|
+
const app = express();
|
|
32
|
+
app.use(express.json());
|
|
33
|
+
|
|
34
|
+
// Initialize CAuth with Express contractor
|
|
35
|
+
const CAuthClient = CAuth({
|
|
36
|
+
dbContractor: new PrismaContractor(prismaClient),
|
|
37
|
+
routeContractor: new ExpressContractor(),
|
|
38
|
+
roles: ['USER', 'ADMIN'],
|
|
39
|
+
jwtConfig: {
|
|
40
|
+
accessTokenSecret: process.env.ACCESS_TOKEN_SECRET!,
|
|
41
|
+
refreshTokenSecret: process.env.REFRESH_TOKEN_SECRET!,
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// authentication routes
|
|
46
|
+
app.post('/register', CAuthClient.Routes.Register())
|
|
47
|
+
|
|
48
|
+
app.post('/login', CAuthClient.Routes.Login())
|
|
49
|
+
|
|
50
|
+
// Using the Guard to extract the id from the user's request
|
|
51
|
+
app.post('/change-password', CAuthClient.Guard(), (req: Request, res: Response) => CAuth.Guard(), CAuthClient.Routes.ChangePassword(req.cauth?.id!)(req, res))
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
app.post('/refresh', CAuthClient.Routes.Refresh())
|
|
55
|
+
|
|
56
|
+
app.post('/logout', CAuthClient.Routes.Logout())
|
|
57
|
+
|
|
58
|
+
app.post('/login-with-code', async (req: Request, res: Response) => {
|
|
59
|
+
const result = await CAuthClient.FN.LoginWithOTP({ phoneNumber: req.body.phone, code: req.body.code })
|
|
60
|
+
|
|
61
|
+
return res.send(result)
|
|
62
|
+
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// Protected route example
|
|
66
|
+
app.get('/protected', CAuthClient.Guard(), (req, res) => {
|
|
67
|
+
// User data is available in req.user
|
|
68
|
+
res.json({ message: 'Protected data', user: req.user });
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Role-based protection
|
|
72
|
+
app.get('/admin', Guard(['ADMIN']), (req, res) => {
|
|
73
|
+
res.json({ message: 'Admin only', user: req.user });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
app.listen(3000, () => {
|
|
77
|
+
console.log('Server running on port 3000');
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## API Reference
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
### Guard Middleware
|
|
85
|
+
|
|
86
|
+
The `Guard` middleware protects routes and adds user data to the request object:
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
// Protect route for authenticated users
|
|
90
|
+
app.get('/profile', CAuthCliebt.Guard(), (req, res) => {
|
|
91
|
+
const data = req.cauth; // TypeScript knows user exists
|
|
92
|
+
res.json({ user });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Protect route for specific roles
|
|
96
|
+
app.get('/admin', Guard(['ADMIN']), (req, res) => {
|
|
97
|
+
res.json({ message: 'Admin access granted' });
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Request Object
|
|
102
|
+
|
|
103
|
+
The middleware augments the Express `Request` object with user data:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
interface AuthenticatedRequest extends Request {
|
|
107
|
+
cauth: {
|
|
108
|
+
id: string;
|
|
109
|
+
role: string;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Error Handling
|
|
115
|
+
|
|
116
|
+
Common error status codes:
|
|
117
|
+
|
|
118
|
+
- 400: Invalid request data
|
|
119
|
+
- 401: Unauthorized (invalid credentials)
|
|
120
|
+
- 403: Forbidden (insufficient permissions)
|
|
121
|
+
- 404: Account not found
|
|
122
|
+
- 409: Duplicate account
|
|
123
|
+
- 422: Invalid OTP code
|
|
124
|
+
|
|
125
|
+
## Development
|
|
126
|
+
|
|
127
|
+
### Prerequisites
|
|
128
|
+
|
|
129
|
+
- Node.js >= 18
|
|
130
|
+
- TypeScript >= 5.9
|
|
131
|
+
- Express.js >= 4.18
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
## License
|
|
135
|
+
|
|
136
|
+
MIT License - see LICENSE file for details.
|
|
137
|
+
|
|
138
|
+
## Support
|
|
139
|
+
|
|
140
|
+
For issues and feature requests, please visit the [GitHub repository](https://github.com/jonace-mpelule/cauth).
|