mysql - Trying to pass an authenticated user to DB controller in nodejs with bookshelf -
i creating web app first allows user login using credentials stored in mysql database. then, when go /crm trying pass field value, specifies database user has access, through instantiate bookshelf/knex database connection.
crmdb.js
var knex= require('knex')({ client: 'mysql', connection:{ host: 'localhost', // host user: 'the user', // database user password: 'the password, // database password database: req.user.db, // database name per user.db field charset: 'utf8_general_ci' } }); var bookshelf = require('bookshelf')(knex); module.exports.crmdb = bookshelf;
can this? put in route?
var crm = function (req, res, next) { if(req.isauthenticated()){ } else { res.redirect('/signin'); } }
in route.js
var crm = function (req, res, next) { if(req.isauthenticated()){ var user = req.user; module.exports = { user:user }; var crmmodel = require('../server/models/crm'); } else { res.redirect('/signin'); } }
then in crmdb.js
var db = require('../../client/routes').user.tojson().db; var knex= require('knex')({ client: 'mysql', connection:{ host: 'localhost', // host user: 'user', // database user password: 'password', // database password database: db, charset: 'utf8_general_ci' } });