GithubHelp home page GithubHelp logo

solzimer / session-pouchdb-store Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 9.0 222 KB

PouchDB express session store. Can do realtime session data synchronization via PouchDB server

JavaScript 100.00%
couchdb express expressjs middleware pouchdb realtime session store synchronization

session-pouchdb-store's People

Contributors

joepotentier avatar solzimer avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

session-pouchdb-store's Issues

req.session.passport does not get set

The below code works great when using the default MemoryStore or other stores such as ´memorystore´ or ´session-file-store´session storage but when I set PouchSessionas the session store req.session.passport no longer gets set. I can see that user.user is getting saved on the server but for ever reason req.session.passport no longer exists/gets created. I don't really have much experience with development so there is a good chance I am doing something wrong but since the other stores work fine I am wondering if something could be up with ´session-pouchdb-store´

const express = require('express');
const session = require('express-session');
const PouchSession = require("session-pouchdb-store");
const app = express();
const nano = require('nano');
const passport = require('passport');
const PORT = 5000;


require('./passport')(passport);
app.use(express.urlencoded({extended: true}));

app.use(session({
  secret: 'thisismysecret',
  resave: false,
  saveUninitialized: false,
  cookie: { secure: false},
    store : new PouchSession('http://admin:password@localhost:5984/sessions')
}));

app.use(passport.initialize());
app.use(passport.session());

app.use('/', require('./routes/index'));
app.use('/users', require('./routes/users'));


app.listen(PORT, () => console.log('server running...'));

passport.js

const LocalStrategy = require('passport-local').Strategy;
const nano = require('nano')('http://admin:password@localhost:5984');
const users = nano.use('users');
const bcrypt = require('bcrypt');

module.exports = function(passport) {
  passport.use(new LocalStrategy(
  function(username, password, done) {
    users.view('auth', 'auth', {'key': username, 'include_docs': true})
    .then(dbresponse => {
      if (dbresponse.rows.length === 1) {
        const user = dbresponse.rows[0].doc;
        bcrypt.compare(password, user.password, function(err, result) {
          if(result === true) {
            done(null, user)
          } else {
            done(null, false)
          }
        });
      } else {
        done(null, false);
      }
    })
    .catch(err => {
      console.log(err);
      done(null, false);
    })
  }
));

passport.serializeUser(function(user, done) {
  console.log("serializeUser");
  done(null, user.user);
});

passport.deserializeUser(function(id, done) {
  console.log("deserializeUser");
  done(null, 'test');
});

Sessions are not destroyed

When calling destroy() on a session it is not destroyed, due to following error:

TypeError: Cannot read property '_id' of undefined
    at PouchDB.<anonymous> (/home/tionis/dev/ep/webapp/node_modules/pouchdb/lib/index.js:1884:26)
    at PouchDB.<anonymous> (/home/tionis/dev/ep/webapp/node_modules/pouchdb/lib/index.js:288:21)
    at PouchDB.<anonymous> (/home/tionis/dev/ep/webapp/node_modules/argsarray/index.js:14:18)
    at /home/tionis/dev/ep/webapp/node_modules/pouchdb/lib/index.js:227:21
    at new Promise (<anonymous>)
    at PouchDB.<anonymous> (/home/tionis/dev/ep/webapp/node_modules/pouchdb/lib/index.js:214:19)
    at PouchDB.remove (/home/tionis/dev/ep/webapp/node_modules/argsarray/index.js:14:18)
    at SessionStore.destroy (/home/tionis/dev/ep/webapp/node_modules/session-pouchdb-store/lib/store.js:136:21)
    at processTicksAndRejections (node:internal/process/task_queues:93:5)

set function breaks session cookie

The following code is causing all of the cookie parameters to be removed from the Set-Cookie header when the cookie is initially set by express-sessions.

if(session.cookie && session.cookie.toJSON) {
session.cookie = session.cookie.toJSON();
}

By turning the cookie object into a normal JSON object all of the computed properties on the object are lost. This causes an issue in express-sessions since it uses the computed data property to fill in the Set-Cookie header.

Ref: the data property

https://github.com/expressjs/session/blob/e18fb51d2d403bf3b5bd4e498f621dee4a52db41/session/cookie.js#L117-L127

Ref: where the data property is being used

https://github.com/expressjs/session/blob/e18fb51d2d403bf3b5bd4e498f621dee4a52db41/index.js#L243

Purge timer option has no effect?

Is this store meant to purge old DB records? The purge setting doesn't seem to be used at all in the code.

It'd be nice to not have a couch db filled up with expired session tokens. Is support for purging expired records planned?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.