authbackendpackage 1.1.0 β 1.1.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 +264 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
Hereβs the updated **README** file with the mention of successful testing on your website **PulseTalk**:
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# π AuthBackendPackage
|
|
6
|
+
|
|
7
|
+
A flexible and plug-and-play authentication module for [Node.js](w) applications. Provides features such as [OTP](w)-based verification, [JWT](w) authentication, email verification, password reset, and user profile management.
|
|
8
|
+
|
|
9
|
+
β
**Successfully tested and used in production at:**
|
|
10
|
+
π [https://pulsetalk-6lrk.onrender.com](https://pulsetalk-6lrk.onrender.com)
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## π§ Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm i authbackendpackage
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## π¦ Module Setup
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
// index.js or app.js
|
|
26
|
+
import express from "express";
|
|
27
|
+
import { createAuthModule } from "authbackendpackage";
|
|
28
|
+
import userModel from "./models/user.model.js";
|
|
29
|
+
import cloudinary from "./lib/cloudinary.js";
|
|
30
|
+
|
|
31
|
+
const app = express();
|
|
32
|
+
|
|
33
|
+
const auth = createAuthModule({
|
|
34
|
+
userModel,
|
|
35
|
+
cloudinaryInstance: cloudinary,
|
|
36
|
+
jwtSecret: process.env.JWT_SECRET,
|
|
37
|
+
mailUser: process.env.MY_MAIL,
|
|
38
|
+
mailPass: process.env.MY_PASSWORD,
|
|
39
|
+
env: process.env.NODE_ENV,
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## βοΈ Cloudinary Configuration
|
|
46
|
+
|
|
47
|
+
Create an account on [Cloudinary](https://cloudinary.com/), generate API credentials, and store them in your `.env` file.
|
|
48
|
+
|
|
49
|
+
**Cloudinary Instance:**
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import { config } from "dotenv";
|
|
53
|
+
import { v2 as cloudinary } from "cloudinary";
|
|
54
|
+
config();
|
|
55
|
+
|
|
56
|
+
cloudinary.config({
|
|
57
|
+
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
|
|
58
|
+
api_key: process.env.CLOUDINARY_API_KEY,
|
|
59
|
+
api_secret: process.env.CLOUDINARY_API_SECRET,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
export default cloudinary;
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## π JWT Secret
|
|
68
|
+
|
|
69
|
+
Set a secure `JWT_SECRET` string in your `.env` file.
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## π§ Mail Setup
|
|
74
|
+
|
|
75
|
+
Generate an **App Password** from your Gmail settings and store it in `.env`.
|
|
76
|
+
|
|
77
|
+
π Follow this [Gmail App Password Guide](https://itsupport.umd.edu/itsupport?id=kb_article_view&sysparm_article=KB0015112)
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## π€ User Model Example
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
import mongoose from 'mongoose';
|
|
85
|
+
|
|
86
|
+
const userSchema = new mongoose.Schema({
|
|
87
|
+
email: { type: String, required: true, unique: true },
|
|
88
|
+
name: { type: String, required: true },
|
|
89
|
+
password: { type: String, required: true },
|
|
90
|
+
profilePicture: { type: String, default: "" },
|
|
91
|
+
}, { timestamps: true });
|
|
92
|
+
|
|
93
|
+
const User = mongoose.model('User', userSchema);
|
|
94
|
+
export default User;
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## π Routes Setup
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
app.post("/api/send-otp", auth.sendOtp);
|
|
103
|
+
app.post("/api/verify-otp", auth.verifyOTP);
|
|
104
|
+
app.post("/api/signup", auth.signup);
|
|
105
|
+
app.post("/api/login", auth.login);
|
|
106
|
+
app.post("/api/logout", auth.logout);
|
|
107
|
+
app.put("/api/update-profile", auth.updateProfile);
|
|
108
|
+
app.get("/api/check-auth", auth.checkAuth);
|
|
109
|
+
app.post("/api/forgot-password", auth.forgotPassword);
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## π‘οΈ Middleware: Protect Route
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
import jwt from "jsonwebtoken";
|
|
118
|
+
import user from "../models/user.model.js";
|
|
119
|
+
import dotenv from "dotenv";
|
|
120
|
+
dotenv.config();
|
|
121
|
+
|
|
122
|
+
export const protectRoute = async (req, res, next) => {
|
|
123
|
+
try {
|
|
124
|
+
const token = req.cookies.jwt;
|
|
125
|
+
if (!token) {
|
|
126
|
+
return res.status(401).json({ message: "Not authorized - No token provided" });
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const decoded = jwt.verify(token, process.env.JWT_SECRET || "shreyash5506");
|
|
130
|
+
const foundUser = await user.findById(decoded.userId).select("-password");
|
|
131
|
+
|
|
132
|
+
if (!foundUser) {
|
|
133
|
+
return res.status(401).json({ message: "Not authorized - User not found" });
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
req.user = foundUser;
|
|
137
|
+
next();
|
|
138
|
+
} catch (error) {
|
|
139
|
+
console.error("Auth middleware error:", error);
|
|
140
|
+
res.status(401).json({ message: "Not authorized - Invalid token" });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## π§ Features
|
|
148
|
+
|
|
149
|
+
* β
OTP verification via email (SMTP)
|
|
150
|
+
* β
Signup with verified OTP
|
|
151
|
+
* β
Secure login with JWT
|
|
152
|
+
* β
Profile update with image support (Cloudinary)
|
|
153
|
+
* β
Forgot password with [bcrypt](w)
|
|
154
|
+
* β
Cookie-based logout
|
|
155
|
+
* β
Middleware-ready routes
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## π§ͺ Example `.env`
|
|
160
|
+
|
|
161
|
+
```env
|
|
162
|
+
MY_MAIL=your-email@gmail.com
|
|
163
|
+
MY_PASSWORD=your-app-password
|
|
164
|
+
JWT_SECRET=your-secret-key
|
|
165
|
+
NODE_ENV=development
|
|
166
|
+
CLOUDINARY_CLOUD_NAME=your-cloud-name
|
|
167
|
+
CLOUDINARY_API_KEY=your-api-key
|
|
168
|
+
CLOUDINARY_API_SECRET=your-api-secret
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## π₯ Request Examples
|
|
174
|
+
|
|
175
|
+
### 1. Send OTP
|
|
176
|
+
|
|
177
|
+
```http
|
|
178
|
+
POST /api/send-otp
|
|
179
|
+
Content-Type: application/json
|
|
180
|
+
{
|
|
181
|
+
"email": "user@example.com"
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### 2. Verify OTP
|
|
186
|
+
|
|
187
|
+
```http
|
|
188
|
+
POST /api/verify-otp
|
|
189
|
+
Content-Type: application/json
|
|
190
|
+
{
|
|
191
|
+
"email": "user@example.com",
|
|
192
|
+
"otp": "123456"
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### 3. Signup
|
|
197
|
+
|
|
198
|
+
```http
|
|
199
|
+
POST /api/signup
|
|
200
|
+
Content-Type: application/json
|
|
201
|
+
{
|
|
202
|
+
"email": "user@example.com",
|
|
203
|
+
"password": "your-password",
|
|
204
|
+
"name": "User Name"
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### 4. Login
|
|
209
|
+
|
|
210
|
+
```http
|
|
211
|
+
POST /api/login
|
|
212
|
+
Content-Type: application/json
|
|
213
|
+
{
|
|
214
|
+
"email": "user@example.com",
|
|
215
|
+
"password": "your-password"
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### 5. Update Profile
|
|
220
|
+
|
|
221
|
+
```http
|
|
222
|
+
PUT /api/update-profile
|
|
223
|
+
Content-Type: application/json
|
|
224
|
+
{
|
|
225
|
+
"name": "New Name",
|
|
226
|
+
"profilePicture": "base64encodedImageOrUrl"
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### 6. Forgot Password
|
|
231
|
+
|
|
232
|
+
```http
|
|
233
|
+
POST /api/forgot-password
|
|
234
|
+
Content-Type: application/json
|
|
235
|
+
{
|
|
236
|
+
"email": "user@example.com",
|
|
237
|
+
"newPassword": "new-secure-password"
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## π Cookie-Based JWT Auth
|
|
244
|
+
|
|
245
|
+
Authentication is done using `httpOnly` cookies which automatically expire after 7 days for enhanced security.
|
|
246
|
+
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
## π Live Usage Demo
|
|
250
|
+
|
|
251
|
+
β
**Successfully running on:**
|
|
252
|
+
π [https://pulsetalk-6lrk.onrender.com](https://pulsetalk-6lrk.onrender.com)
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## π License
|
|
257
|
+
|
|
258
|
+
Licensed under [Apache-2.0](w).
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
Built with β€οΈ by the **Shreyash Team**
|
|
263
|
+
|
|
264
|
+
---
|