File tree Expand file tree Collapse file tree 4 files changed +45
-21
lines changed
Expand file tree Collapse file tree 4 files changed +45
-21
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 33// src/scripts/seedAdmin.js
44import dotenv from 'dotenv' ;
55import connectDB from '../config/db.js' ;
6- import User from ' ../models/user.model.js' ;
6+ import ensureAdmin from " ../utils/ensureAdmin.js" ;
77
88dotenv . config ( ) ;
99
1010const 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 ) ;
Original file line number Diff line number Diff line change 11import http from 'http' ;
22import app from './app.js' ;
33import connectDB from './config/db.js' ;
4+ import ensureAdmin from "./utils/ensureAdmin.js" ;
45
56const PORT = process . env . PORT || 5000 ;
67
@@ -9,6 +10,7 @@ const server = http.createServer(app); // Create HTTP server
910const startServer = async ( ) => {
1011 try {
1112 await connectDB ( ) ;
13+ await ensureAdmin ( ) ;
1214 server . listen ( PORT , ( ) => {
1315 console . log ( `Server is running on PORT ${ PORT } ` ) ;
1416 } ) ;
Original file line number Diff line number Diff line change 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 ;
You can’t perform that action at this time.
0 commit comments