Skip to content

Commit fd08ca2

Browse files
committed
fix supper admin create
1 parent 24abd0b commit fd08ca2

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

backend/src/models/user.model.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ const userSchema = new mongoose.Schema(
3131
);
3232

3333
// Hash password before saving
34-
userSchema.pre("save", async function (next) {
34+
userSchema.pre("save", async function () {
3535
if (!this.isModified("password")) {
36-
return next();
36+
return;
3737
}
3838
const salt = await bcrypt.genSalt(10);
3939
this.password = await bcrypt.hash(this.password, salt);
40-
next();
4140
});
4241

4342
// Compare password

backend/src/scripts/seedAdmin.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,14 @@
33
// src/scripts/seedAdmin.js
44
import dotenv from 'dotenv';
55
import connectDB from '../config/db.js';
6-
import User from '../models/user.model.js';
6+
import ensureAdmin from "../utils/ensureAdmin.js";
77

88
dotenv.config();
99

1010
const seedAdmin = async () => {
1111
try {
1212
await connectDB();
13-
14-
// check if an admin already exists
15-
const adminExists = await User.findOne({ role: 'admin' });
16-
if (adminExists) {
17-
console.log(`Admin already exists: ${adminExists.email}`);
18-
process.exit();
19-
}
20-
21-
// create admin from env
22-
await User.create({
23-
name: process.env.ADMIN_NAME,
24-
email: process.env.ADMIN_EMAIL,
25-
password: process.env.ADMIN_PASSWORD,
26-
role: 'admin',
27-
});
28-
29-
console.log('Admin user created successfully!');
13+
await ensureAdmin();
3014
process.exit();
3115
} catch (error) {
3216
console.error('Error seeding admin:', error.message);

backend/src/server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import http from 'http';
22
import app from './app.js';
33
import connectDB from './config/db.js';
4+
import ensureAdmin from "./utils/ensureAdmin.js";
45

56
const PORT = process.env.PORT || 5000;
67

@@ -9,6 +10,7 @@ const server = http.createServer(app); // Create HTTP server
910
const startServer = async () => {
1011
try {
1112
await connectDB();
13+
await ensureAdmin();
1214
server.listen(PORT, () => {
1315
console.log(`Server is running on PORT ${PORT}`);
1416
});

backend/src/utils/ensureAdmin.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import User from "../models/user.model.js";
2+
3+
const ensureAdmin = async () => {
4+
const name = process.env.ADMIN_NAME;
5+
const email = process.env.ADMIN_EMAIL;
6+
const password = process.env.ADMIN_PASSWORD;
7+
8+
if (!name || !email || !password) {
9+
console.log(
10+
"Admin seed skipped: ADMIN_NAME, ADMIN_EMAIL, or ADMIN_PASSWORD is missing."
11+
);
12+
return;
13+
}
14+
15+
const existingAdmin = await User.findOne({ role: "admin" });
16+
if (existingAdmin) {
17+
return;
18+
}
19+
20+
const existingByEmail = await User.findOne({ email });
21+
if (existingByEmail) {
22+
existingByEmail.role = "admin";
23+
existingByEmail.name = name;
24+
await existingByEmail.save();
25+
console.log(`Admin role granted to existing user: ${email}`);
26+
return;
27+
}
28+
29+
await User.create({
30+
name,
31+
email,
32+
password,
33+
role: "admin",
34+
});
35+
36+
console.log(`Admin user created: ${email}`);
37+
};
38+
39+
export default ensureAdmin;

0 commit comments

Comments
 (0)