Before creating a dashboard in the backend, we need to create a user who will take control of the dashboard. That user will be called “superuser”. There are many ways to create this superuser, one way is to create a function in the model section to create that user and store it in the MongoDB database. To run the code in this function, we could import it into getLogin controller. Doing so, when we make a request to get into login page, that superuser will be created.
// model/user/superUser.js
// npm install bcryptjs
import bcryptjs from 'bcryptjs'
export default async (req)=>{
const id = Date.now() + Math.round(Math.random() * 1E9).toString()
const hashPassword = bcryptjs.hashSync('xxxxxxxxxxxxxxxxxxxxxx', 12)
let user = {
id: id,
title: 'superUser',
password: hashPassword,
email: 'superuser@khmerweb.app',
role: 'Admin',
thumb: '',
info: '',
video: '',
date: ''
}
await req.mydb.collection("users").insertOne(user)
}
// controller/login/getLogin.js
import config from '../../config.js'
export default async (req,res)=>{
const setting = await config()
setting.pageTitle = 'ទំព័រចុះឈ្មោះ'
setting.route = '/login'
// to be deleted after superUser was created
const module = await import('../../model/user/superUser.js')
module.default(req)
res.render('base',{data:setting})
}
After the operation is done, we could check our MongoDB database on MongoDB Atlas to verify whether the superuser was successfully created or not.