-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
97 lines (91 loc) · 2.81 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var express = require('express');
const Emp = require('./Models/emp')
const AdminBro = require('admin-bro')
const AdminBroMongoose = require('@admin-bro/mongoose')
const AdminBroExpress = require('@admin-bro/express')
var app = express();
const mongoose = require('mongoose');//Routes
const User = require('./Models/user')
const AdminBroExpressjs = require('admin-bro-expressjs')
app.get('/', function (req, res) {
res.send('Hello World!');
});
//Database
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true});
mongoose.connection.once('open',function(){
console.log('Database connected Successfully');
}).on('error',function(err){
console.log('Error', err);
})
//Admin Bro
const canModifyUsers = ({ currentAdmin }) => currentAdmin && currentAdmin.role === 'admin'
const canEditEmp = ({ currentAdmin, record }) => {
return currentAdmin && (
currentAdmin.role === 'admin'
)
}
AdminBro.registerAdapter(AdminBroMongoose)
const AdminBroOptions = {
resources:
[
{
resource: Emp,
options: {
properties: {
ownerId: { isVisible: { edit: false, show: true, list: true, filter: true } }
},
actions: {
edit: { isAccessible: canEditEmp },
delete: { isAccessible: canEditEmp },
new: { isAccessible: canEditEmp },
}
}},
{
resource: User,
options: {
properties: {
encryptedPassword: { isVisible: false },
password: {
type: 'string',
isVisible: {
list: false, edit: true, filter: false, show: false,
},
},
},
actions: {
new: {
before: async (request) => {
if(request.payload.record.password) {
request.payload.record = {
...request.payload.record,
encryptedPassword: await bcrypt.hash(request.payload.record.password, 10),
password: undefined,
}
}
return request
},
},
edit: { isAccessible: canModifyUsers },
delete: { isAccessible: canModifyUsers },
new: { isAccessible: canModifyUsers },
}
}
}],
}
const adminBro = new AdminBro(AdminBroOptions)
const router = AdminBroExpressjs.buildAuthenticatedRouter(adminBro, {
authenticate: async (email, password) => {
const user = await User.findOne({ email })
if (user) {
if (password === user.encryptedPassword) {
return user
}
}
return false
},
cookiePassword: 'session Key',
})
app.use(adminBro.options.rootPath, router)
app.listen(8000, function () {
console.log('Listening to Port 8000');
});