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