create-express-kickstart 1.2.7 → 1.2.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/.env.example CHANGED
@@ -1,3 +1,4 @@
1
+ # Basic Environment Variables
1
2
  PORT=8000
2
3
  MONGODB_URI=mongodb://localhost:27017/
3
4
  CORS_ORIGIN=*
@@ -6,3 +7,10 @@ NODE_ENV=development
6
7
  # Rate Limiting
7
8
  RATE_LIMIT_WINDOW_MS=900000 # 15 minutes in milliseconds
8
9
  RATE_LIMIT_MAX=100 # Maximum requests per windowMs
10
+
11
+ # Bcrypt Configuration
12
+ BCRYPT_SALT=10
13
+
14
+ # JWT Configuration
15
+ JWT_SECRET=your_jwt_secret_key
16
+ JWT_EXPIRES_IN=1d
package/README.md CHANGED
@@ -115,10 +115,11 @@ const restrictedRoute = asyncHandler(async (req, res) => {
115
115
  ### `asyncHandler`
116
116
  A wrapper for your async route handlers that eliminates the need for repetitive `try-catch` blocks.
117
117
 
118
- ### `hash.util.js`
119
- If you choose to install the basic JWT Auth boilerplate, we automatically generate a generic hashing utility utilizing `bcryptjs` to help you securely hash and compare data (like passwords) natively.
118
+ ### `jwt.util.js` & `hash.util.js`
119
+ If you choose to install the basic JWT Auth boilerplate, we automatically generate symmetric cryptography utilities wrapping `jsonwebtoken` and `bcryptjs`. This helps you map, hash, and assign JWT secrets synchronously against `.env`.
120
120
  ```javascript
121
121
  import { hashData, compareData } from "#utils/hash.util.js";
122
+ import { generateToken, verifyToken } from "#utils/jwt.util.js";
122
123
 
123
124
  const registerUser = asyncHandler(async (req, res) => {
124
125
  const hashedPassword = await hashData("supersecret123");
@@ -127,7 +128,10 @@ const registerUser = asyncHandler(async (req, res) => {
127
128
 
128
129
  const loginUser = asyncHandler(async (req, res) => {
129
130
  const isMatch = await compareData("supersecret123", user.hashedPassword);
130
- // ...
131
+
132
+ // Generate JWT natively hooked up to process.env.JWT_SECRET
133
+ const token = generateToken({ id: user._id, role: "user" });
134
+ return res.json({ token });
131
135
  });
132
136
  ```
133
137
 
package/bin/cli.js CHANGED
@@ -148,8 +148,16 @@ async function init() {
148
148
  );
149
149
 
150
150
  // Append JWT secret and Salt Rounds to env example
151
- fs.appendFileSync(path.join(projectPath, '.env.example'), '\nJWT_SECRET=supersecretjwtkey123\nBCRYPT_SALT=10\n');
152
- fs.appendFileSync(path.join(projectPath, '.env'), '\nJWT_SECRET=supersecretjwtkey123\nBCRYPT_SALT=10\n');
151
+ const authEnvConfig = `
152
+ # Bcrypt Configuration
153
+ BCRYPT_SALT=10
154
+
155
+ # JWT Configuration
156
+ JWT_SECRET=supersecretjwtkey123
157
+ JWT_EXPIRES_IN=1d
158
+ `;
159
+ fs.appendFileSync(path.join(projectPath, '.env.example'), authEnvConfig);
160
+ fs.appendFileSync(path.join(projectPath, '.env'), authEnvConfig);
153
161
 
154
162
  const utilsPath = path.join(projectPath, 'src', 'utils');
155
163
  if (!fs.existsSync(utilsPath)) {
@@ -167,6 +175,20 @@ export const hashData = async (data, saltRounds = process.env.BCRYPT_SALT) => {
167
175
  export const compareData = async (data, hashedData) => {
168
176
  return await bcrypt.compare(data, hashedData);
169
177
  };
178
+ `
179
+ );
180
+
181
+ fs.writeFileSync(
182
+ path.join(utilsPath, 'jwt.util.js'),
183
+ `import jwt from "jsonwebtoken";
184
+
185
+ export const generateToken = (payload, expiresIn = process.env.JWT_EXPIRES_IN || "1d") => {
186
+ return jwt.sign(payload, process.env.JWT_SECRET, { expiresIn });
187
+ };
188
+
189
+ export const verifyToken = (token) => {
190
+ return jwt.verify(token, process.env.JWT_SECRET);
191
+ };
170
192
  `
171
193
  );
172
194
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-express-kickstart",
3
- "version": "1.2.7",
3
+ "version": "1.2.8",
4
4
  "description": "Production-ready CLI starter for Express APIs",
5
5
  "main": "bin/cli.js",
6
6
  "bin": {
@@ -1,5 +1,31 @@
1
+ import { asyncHandler } from "#utils/asyncHandler.js";
2
+ import { ApiResponse } from "#utils/ApiResponse.js";
3
+ import { generateToken } from "#utils/jwt.util.js";
4
+ import { hashData, compareData } from "#utils/hash.util.js";
5
+
1
6
  export const authController = {
2
- login: (req, res) => res.json({ message: "Login logic goes here." }),
3
- register: (req, res) => res.json({ message: "Register logic goes here." }),
4
- profile: (req, res) => res.json({ message: "Protected profile data." })
5
- };
7
+ login: asyncHandler(async (req, res) => {
8
+ // In a real app, you would fetch user from database and compare passwords here
9
+ // Example: const isMatch = await compareData(req.body.password, user.password);
10
+
11
+ // For now, let's just generate a mock token
12
+ const token = generateToken({ id: 1, role: "user" });
13
+
14
+ return res.status(200).json(
15
+ new ApiResponse(200, { token }, "Login successful")
16
+ );
17
+ }),
18
+
19
+ register: asyncHandler(async (req, res) => {
20
+ // Example: const hashedPassword = await hashData(req.body.password);
21
+ return res.status(201).json(
22
+ new ApiResponse(201, {}, "Register logic goes here")
23
+ );
24
+ }),
25
+
26
+ profile: asyncHandler(async (req, res) => {
27
+ return res.status(200).json(
28
+ new ApiResponse(200, { user: req.user }, "Protected profile data retrieved successfully.")
29
+ );
30
+ })
31
+ };
@@ -1,5 +1,22 @@
1
+ import { verifyToken } from "#utils/jwt.util.js";
2
+ import { ApiError } from "#utils/ApiError.js";
3
+
1
4
  export const authMiddleware = (req, res, next) => {
2
- // Add JWT verification logic here
3
- console.log('Verifying token...');
4
- next();
5
- };
5
+ const authHeader = req.headers['authorization'];
6
+ if (!authHeader) {
7
+ return next(new ApiError(401, "Authorization header missing."));
8
+ }
9
+
10
+ const token = authHeader.split(' ')[1]; // Assuming "Bearer <token>"
11
+ if (!token) {
12
+ return next(new ApiError(401, "Token missing."));
13
+ }
14
+
15
+ try {
16
+ const decoded = verifyToken(token);
17
+ req.user = decoded;
18
+ next();
19
+ } catch (error) {
20
+ return next(new ApiError(403, "Invalid or expired token."));
21
+ }
22
+ };