secure-auth-kit 1.0.1 → 1.0.2
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 +143 -0
- package/package.json +9 -3
package/README.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# Secure Auth Kit
|
|
2
|
+
|
|
3
|
+
Authentication toolkit for Express.js and MongoDB.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install secure-auth-kit
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import express from 'express';
|
|
19
|
+
import mongoose from 'mongoose';
|
|
20
|
+
import { secureAuth } from 'secure-auth-kit';
|
|
21
|
+
import { User } from './models/User.js';
|
|
22
|
+
|
|
23
|
+
const app = express();
|
|
24
|
+
|
|
25
|
+
secureAuth(app, {
|
|
26
|
+
userModel: User,
|
|
27
|
+
jwt: {
|
|
28
|
+
secret: 'your_jwt_secret',
|
|
29
|
+
accessTokenExpiry: '15m', // default
|
|
30
|
+
refreshTokenExpiry: '7d', // default
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
app.listen(3000);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
This registers the following routes under `/auth` (configurable via `routePrefix`):
|
|
38
|
+
|
|
39
|
+
| Method | Route | Auth required |
|
|
40
|
+
| ------ | -------------- | ------------- |
|
|
41
|
+
| POST | /auth/register | No |
|
|
42
|
+
| POST | /auth/login | No |
|
|
43
|
+
| GET | /auth/me | Yes |
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## User Model Requirements
|
|
48
|
+
|
|
49
|
+
Your Mongoose schema **must** have `email` and `password` fields. The package validates this at startup and throws a descriptive error if either is missing.
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
// models/User.js
|
|
53
|
+
import mongoose from 'mongoose';
|
|
54
|
+
|
|
55
|
+
const userSchema = new mongoose.Schema({
|
|
56
|
+
email: {
|
|
57
|
+
type: String,
|
|
58
|
+
required: true,
|
|
59
|
+
unique: true,
|
|
60
|
+
},
|
|
61
|
+
password: {
|
|
62
|
+
type: String,
|
|
63
|
+
required: true,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
export const User = mongoose.model('User', userSchema);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`secure-auth-kit` will hash passwords on register and compare them on login - **never** store plaintext passwords yourself.
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Configuration
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
secureAuth(app, {
|
|
78
|
+
userModel: User,
|
|
79
|
+
|
|
80
|
+
jwt: {
|
|
81
|
+
secret: 'your_jwt_secret',
|
|
82
|
+
accessTokenExpiry: '15m', // optional, default: '15m'
|
|
83
|
+
refreshTokenExpiry: '7d', // optional, default: '7d'
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
routePrefix: '/auth', // optional, default: '/auth'
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## `authenticate` Middleware
|
|
93
|
+
|
|
94
|
+
Protect any route by importing `authenticate`:,
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { authenticate } from 'secure-auth-kit';
|
|
98
|
+
|
|
99
|
+
app.get('/protected', authenticate, (req, res) => {
|
|
100
|
+
res.json({ user: req.user });
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## API Reference
|
|
107
|
+
|
|
108
|
+
### Routes
|
|
109
|
+
|
|
110
|
+
**POST /auth/register**
|
|
111
|
+
|
|
112
|
+
```json
|
|
113
|
+
{
|
|
114
|
+
"email": "user@example.com",
|
|
115
|
+
"password": "Secret@123"
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Returns `{ user, tokens: { accessToken, refreshToken }}`
|
|
120
|
+
|
|
121
|
+
**POST /auth/login**
|
|
122
|
+
|
|
123
|
+
```json
|
|
124
|
+
{
|
|
125
|
+
"email": "user@example.com",
|
|
126
|
+
"password": "Secret@123"
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Returns `{ user, tokens: { accessToken, refreshToken }}`
|
|
131
|
+
|
|
132
|
+
**GET /auth/me** _(requires Bearer token)_
|
|
133
|
+
</br>
|
|
134
|
+
Returns the current user (sanitized, no password).
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## Security Notes
|
|
139
|
+
|
|
140
|
+
- Passwords are hashed with **bcrypt** (10 salt rounds).
|
|
141
|
+
- Access tokens default to **15 min** expiry; refresh tokens to **7 days**.
|
|
142
|
+
|
|
143
|
+
---
|
package/package.json
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "secure-auth-kit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Authentication toolkit for Express and MongoDB",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/SwapnilSahare45/secure-auth-kit"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://github.com/swapnil-sahare/secure-auth-kit#readme",
|
|
10
|
+
"author": "Swapnil Sahare",
|
|
5
11
|
"type": "module",
|
|
6
12
|
"main": "./dist/index.js",
|
|
7
13
|
"types": "./dist/index.d.ts",
|
|
@@ -12,7 +18,8 @@
|
|
|
12
18
|
}
|
|
13
19
|
},
|
|
14
20
|
"files": [
|
|
15
|
-
"dist"
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md"
|
|
16
23
|
],
|
|
17
24
|
"engines": {
|
|
18
25
|
"node": ">=18"
|
|
@@ -28,7 +35,6 @@
|
|
|
28
35
|
"typescript",
|
|
29
36
|
"auth"
|
|
30
37
|
],
|
|
31
|
-
"license": "MIT",
|
|
32
38
|
"peerDependencies": {
|
|
33
39
|
"express": "^5.0.0",
|
|
34
40
|
"mongoose": "^8.0.0"
|