backend-scaffold-cli 1.7.0 → 1.9.0
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/bin/cli.js +14 -6
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
const { program } = require('commander');
|
|
@@ -162,6 +162,7 @@ const createConfigFiles = (projectPath, answers) => {
|
|
|
162
162
|
mongoose: '^7.0.0',
|
|
163
163
|
dotenv: '^16.0.3',
|
|
164
164
|
cors: '^2.8.5',
|
|
165
|
+
helmet: '^7.1.0',
|
|
165
166
|
...(answers.useAuth && { jsonwebtoken: '^9.0.0' , bcryptjs: '^2.4.3'}),
|
|
166
167
|
...(answers.useValidation && { joi: '^17.9.0' }),
|
|
167
168
|
...(answers.useRateLimit && { 'express-rate-limit': '^6.7.0' }),
|
|
@@ -197,6 +198,7 @@ ${answers.useDNSFix ? '\n# DNS Configuration (for MongoDB connection issues)\nDN
|
|
|
197
198
|
# JWT Configuration
|
|
198
199
|
JWT_SECRET=your-secret-key-change-this-in-production
|
|
199
200
|
JWT_EXPIRE=7d
|
|
201
|
+
${answers.useAuth ? 'BCRYPT_SALT_ROUNDS=10' : ''}
|
|
200
202
|
|
|
201
203
|
# CORS
|
|
202
204
|
CORS_ORIGIN=http://localhost:3000
|
|
@@ -399,11 +401,13 @@ module.exports=limiter;
|
|
|
399
401
|
const { ErrorHandler } = require('./errorHandler');
|
|
400
402
|
|
|
401
403
|
const authenticateToken = (req, res, next) => {
|
|
402
|
-
const
|
|
404
|
+
const authHeader = req.headers.authorization;
|
|
403
405
|
|
|
404
|
-
if (
|
|
405
|
-
return next(new ErrorHandler('
|
|
406
|
+
if(!authHeader || !authHeader.startsWith('Bearer ')){
|
|
407
|
+
return next(new ErrorHandler('Authorization header is missing or malformed', 401));
|
|
406
408
|
}
|
|
409
|
+
const token = authHeader.split(' ')[1];
|
|
410
|
+
|
|
407
411
|
|
|
408
412
|
try {
|
|
409
413
|
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
|
@@ -558,6 +562,7 @@ module.exports = connectDB;
|
|
|
558
562
|
|
|
559
563
|
const server = `require('dotenv').config();
|
|
560
564
|
const express = require('express');
|
|
565
|
+
const helmet = require('helmet');
|
|
561
566
|
const cors = require('cors');
|
|
562
567
|
const connectDB = require('./config/db');
|
|
563
568
|
const routes = require('./routes');
|
|
@@ -566,11 +571,13 @@ const { errorMiddleware } = require('./middleware/errorHandler');
|
|
|
566
571
|
|
|
567
572
|
const app = express();
|
|
568
573
|
${answers.useRateLimit ? "const limiter = require('./middleware/rateLimiter');" : ''}
|
|
574
|
+
|
|
569
575
|
const PORT = process.env.PORT || 5000;
|
|
570
576
|
|
|
571
577
|
|
|
572
578
|
|
|
573
579
|
// Middleware
|
|
580
|
+
app.use(helmet());
|
|
574
581
|
app.use(cors({
|
|
575
582
|
origin: process.env.CORS_ORIGIN || '*'
|
|
576
583
|
}));
|
|
@@ -666,7 +673,8 @@ function createUserModel(projectPath) {
|
|
|
666
673
|
if (!this.isModified('password')) {
|
|
667
674
|
return next();
|
|
668
675
|
}
|
|
669
|
-
|
|
676
|
+
const salt = Number(process.env.BCRYPT_SALT_ROUNDS || 10);
|
|
677
|
+
this.password = await bcrypt.hash(this.password,salt);
|
|
670
678
|
next();
|
|
671
679
|
});
|
|
672
680
|
|
|
@@ -715,7 +723,7 @@ function createAuthController(projectPath) {
|
|
|
715
723
|
}
|
|
716
724
|
|
|
717
725
|
const user= await User.create({name,email,password});
|
|
718
|
-
const
|
|
726
|
+
const token= generateToken(user._id);
|
|
719
727
|
|
|
720
728
|
res.status(201).json({
|
|
721
729
|
success:true,
|