What's the best way to add a default admin to a Node.js / MongoDB application?
So I have an application developed in Nodejs and using a Mongodb / Mongoose database. In addition to the application, there is an admin panel where administrators can manage all the data added from regular users.
in the user schema I have the following:
role: {
type: String,
default: "user",
enum: ["user", "admin"]
},
My questions are the best / safest way to add one or two admin users where they can login using the login form?
source to share
You can use a script to, say seed.js
, to safely insert as many admin users as possible.
//seed.js
var User = require('../path/to/user.js');
var user = {
name: "Admin User",
email: "admin@gmail.com",
role: "admin"
}
User.create(user, function(e) {
if (e) {
throw e;
}
});
Require seed.js
in server.js
or app.js
script right after connecting to mongodb. Comment out or remove this line when you are done seeding.
require('../path/to/seed');
source to share