SMTL

Die Stadtmeistersteilerliste und das Anmeldeformular - SK Langen e.V.
git clone git://git.oshgnacknak.de/SMTL.git
Log | Files | Refs | README

admin.js (1705B)


      1 const AdminBro = require('admin-bro')
      2 const AdminBroExpress = require('admin-bro-expressjs')
      3 const AdminBroMongoose = require('admin-bro-mongoose')
      4 const bcrypt = require('bcrypt')
      5 
      6 const Player = require('./models/Player')
      7 const User = require('./models/User')
      8 
      9 AdminBro.registerAdapter(AdminBroMongoose)
     10 const adminBro = new AdminBro({
     11 	resources: [
     12 		{
     13 			resource: Player,
     14 			options: {
     15 				properties: {
     16 					created_at: { isVisible: { list: true, filter: true, show: true, edit: false } }
     17 				}
     18 			}
     19 		},
     20 		{
     21 			resource: User,
     22 			options: {
     23 				properties: {
     24 					encrypted_password: {
     25 						isVisible: false,
     26 					},
     27 					password: {
     28 						type: 'password',
     29 						isVisible: {
     30 							list: false, edit: true, filter: false, show: false,
     31 						},
     32 					},
     33 				},
     34 				actions: {
     35 					new: {
     36 						before: async (request) => {
     37 							if(request.payload.password) {
     38 								request.payload = {
     39 									...request.payload,
     40 									encrypted_password: await bcrypt.hash(request.payload.password, 10),
     41 									password: undefined,
     42 								}
     43 							}
     44 							return request
     45 						},
     46 					}
     47 				}
     48 			}
     49 		}
     50 	],
     51 	rootPath: '/admin',
     52 	branding: {
     53 		logo: '/vereinslogo1.gif',
     54 		companyName: 'SMTL Admin',
     55 		softwareBrothers: false	 
     56 	}
     57 })
     58 
     59 const { COOKIE_PASSWORD } = process.env
     60 if (!COOKIE_PASSWORD) {
     61 	throw new Error('process.env.COOKIE_PASSWORD must be set')
     62 }
     63 module.exports = AdminBroExpress.buildAuthenticatedRouter(adminBro, {
     64 	authenticate: async (email, password) => {
     65 		const user = await User.findOne({ email })
     66 		if (user) {
     67 			const match = await bcrypt.compare(password, user.encrypted_password)
     68 			if (match) {
     69 				return user
     70 			}
     71 		}
     72 		return false
     73 	},
     74 	cookiePassword: COOKIE_PASSWORD
     75 })