backend-scaffold-cli 1.2.0 → 1.3.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 +66 -1
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -65,6 +65,9 @@ program
|
|
|
65
65
|
createConfigFiles(projectPath, answers);
|
|
66
66
|
createMiddlewareFiles(projectPath, answers);
|
|
67
67
|
createRouteFiles(projectPath, answers);
|
|
68
|
+
if (answers.useAuth) {
|
|
69
|
+
createUserModel(projectPath);
|
|
70
|
+
}
|
|
68
71
|
|
|
69
72
|
console.log(chalk.cyan(' Initializing git repository...'));
|
|
70
73
|
execSync('git init', { cwd: projectPath });
|
|
@@ -133,7 +136,7 @@ const createConfigFiles = (projectPath, answers) => {
|
|
|
133
136
|
mongoose: '^7.0.0',
|
|
134
137
|
dotenv: '^16.0.3',
|
|
135
138
|
cors: '^2.8.5',
|
|
136
|
-
...(answers.useAuth && { jsonwebtoken: '^9.0.0' }),
|
|
139
|
+
...(answers.useAuth && { jsonwebtoken: '^9.0.0' , bcryptjs: '^2.4.3'}),
|
|
137
140
|
...(answers.useValidation && { joi: '^17.9.0' }),
|
|
138
141
|
...(answers.useRateLimit && { 'express-rate-limit': '^6.7.0' }),
|
|
139
142
|
|
|
@@ -513,3 +516,65 @@ process.on('unhandledRejection', (err) => {
|
|
|
513
516
|
|
|
514
517
|
console.log(chalk.green('Route and config files created'));
|
|
515
518
|
}
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
function createUserModel(projectPath) {
|
|
525
|
+
const userModel = `const mongoose = require('mongoose');
|
|
526
|
+
const bcrypt = require('bcryptjs');
|
|
527
|
+
|
|
528
|
+
const userSchema = new mongoose.Schema(
|
|
529
|
+
{
|
|
530
|
+
name:{
|
|
531
|
+
type: String,
|
|
532
|
+
required: [true, 'Name is required'],
|
|
533
|
+
trim: true,
|
|
534
|
+
},
|
|
535
|
+
email: {
|
|
536
|
+
type: String,
|
|
537
|
+
required: [true, 'Email is required'],
|
|
538
|
+
unique: true,
|
|
539
|
+
lowercase: true,
|
|
540
|
+
trim: true,
|
|
541
|
+
match: [/.+@.+\\..+/, 'Please fill a valid email address'],
|
|
542
|
+
},
|
|
543
|
+
password: {
|
|
544
|
+
type: String,
|
|
545
|
+
required: [true, 'Password is required'],
|
|
546
|
+
minlength: [6, 'Password must be at least 6 characters long'],
|
|
547
|
+
select: false
|
|
548
|
+
},
|
|
549
|
+
},
|
|
550
|
+
{ timestamps: true }
|
|
551
|
+
|
|
552
|
+
);
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
//hash password before saving
|
|
556
|
+
userSchema.pre('save', async function (next) {
|
|
557
|
+
if (!this.isModified('password')) {
|
|
558
|
+
return next();
|
|
559
|
+
}
|
|
560
|
+
this.password = await bcrypt.hash(this.password,10);
|
|
561
|
+
next();
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
//compare password method
|
|
566
|
+
userSchema.methods.comparePassword = async function(candidatePassword) {
|
|
567
|
+
return await bcrypt.compare(candidatePassword, this.password);
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
module.exports = mongoose.model('User', userSchema);
|
|
571
|
+
|
|
572
|
+
`;
|
|
573
|
+
|
|
574
|
+
fs.writeFileSync(
|
|
575
|
+
path.join(projectPath, 'src/models/User.js'),
|
|
576
|
+
userModel
|
|
577
|
+
)
|
|
578
|
+
|
|
579
|
+
console.log(chalk.green('User model created'));
|
|
580
|
+
}
|