Express middleware to retrieve a user session from a drupal 7 cookie
Express middleware for Drupal 7 authentication
Install
npm i express-drupal7-session-middleware
You need to use knex query builder to use this package.
npm i cookie-parser knex
See knex documentation on how to create a new instance.
Usage
In your server definition
const { drupalExpressMiddleware } = require('express-drupal7-session-middleware');
app.httpServer = http.createServer(app);
app.use(cookieParser());
app.use(drupalExpressMiddleware('local.my-website.com', knex));
then in your routes
const { drupalUserAccess, drupalCSRFToken } = require('express-drupal7-session-middleware');
const myRoute = async (req, res) => {
const access = await drupalUserAccess(knex, 'access api myRoute', req.userId);
if (!access) {
return res.status(403).send();
}
console.log(req.cookieSessionText);
const drupalHashSalt = 'fwf3qfwgrbq34h34qeg134g3434g5340f-0f';
const csrfToken = await drupalCSRFToken(knex, req.sessionId, drupalHashSalt, 'services'));
res.json({ csrfToken });
}