-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.js
66 lines (62 loc) · 1.72 KB
/
admin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const express = require("express");
let router = express.Router();
let configure_db = require("./db");
const models = require("./models");
const AdminBro = require("admin-bro");
const AdminBroExpress = require("@admin-bro/express");
const AdminBroMongoose = require("@admin-bro/mongoose");
BASE_URL = process.env.BASE_URL || "project";
/**
* @function
* @inner
* @name adminConfig
* Admin panel configratioj
* add dev and prod setup of admin panel
*/
const configureAdmin = () => {
const connection = configure_db();
const session = require("./middlewares/express-mongo-store");
AdminBro.registerAdapter(AdminBroMongoose);
const adminBro = new AdminBro({
databases: [connection],
rootPath: `/${BASE_URL}/admin`,
loginPath: `/${BASE_URL}/admin/login`,
logoutPath: `/${BASE_URL}/admin/logout`,
});
router = AdminBroExpress.buildAuthenticatedRouter(
adminBro,
{
authenticate: async (email, pass) => {
try {
let admin;
if (process.env.NODE_ENV == "development") {
admin = {
username: "admin",
email: "[email protected]",
};
} else {
admin = await models.User.findByCredentials(email, pass, true);
}
return admin;
} catch (err) {
console.log(err);
return null;
}
},
cookiePassword: process.env.ADMIN_COOKIE_SECRET,
},
router,
{
secret: process.env.SESSION_SECRET,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7,
},
store: session.store,
resave: true,
saveUninitialized: true,
}
);
router = AdminBroExpress.buildRouter(adminBro, router);
return router;
};
module.exports = configureAdmin;