Coder Social home page Coder Social logo

Comments (5)

makuga01 avatar makuga01 commented on September 23, 2024 2

@saraginov I actually found the solution lol
I dug into source of req.session.destroy -> found out that it takes only the fn argument and later passes it to req.sessionStore.destroy(this.id, fn)

The solution in your case would be just to do

exports.logOut = async function(req, res)
{
const {session, sessionID} = req;

const cb = (err) => {
console.log({err});
}

console.log({sessionID});
console.log(cb);

const deletedSession = req.sessionStore.destroy(sessionID, cb);

res.header('Content-Type', 'application/json;charset=UTF-8');
res.header('Access-Control-Allow-Credentials', true);
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);

delete req.session
res.end('Logged Out');
}

Or a little better without using the session.destroy

req.session.destroy(function (err: Error) {
    if (err) {
      res.status(400)
    } else {
      res.clearCookie('connect.sid')
      res.header('Content-Type', 'application/json;charset=UTF-8');
      res.header('Access-Control-Allow-Credentials', true);
      res.header(
      'Access-Control-Allow-Headers',
      'Origin, X-Requested-With, Content-Type, Accept'
      );
      res.status(200)
    }
  })

Bonus

The connect-pg-simple module just runs a simple SQL query when sessionStore.destroy(x, y) is called.

this.query(
        'DELETE FROM ' + this.quotedTable() + ' WHERE sid = $1',
        [sid],
        err => { fn && fn(err); }
      );

My usecase of this was to log user off on every device using a knex/sql query like this

knex('session').select('sid').whereRaw("sess->'passport'->>'user' = ?", [userId]).delete()
select sess->'passport'->>'user' as userid from session where sess->'passport'->>'user' = 'ID here'

from node-connect-pg-simple.

makuga01 avatar makuga01 commented on September 23, 2024 1

Glad to help

from node-connect-pg-simple.

makuga01 avatar makuga01 commented on September 23, 2024

Hey @saraginov! How did you manage to solve this?

from node-connect-pg-simple.

saraginov avatar saraginov commented on September 23, 2024

@makuga01 I didn't, I closed this because I am overwhelmed with other work and don't have time to address it. Feel free to re-open it if you would like.

from node-connect-pg-simple.

saraginov avatar saraginov commented on September 23, 2024

Thank you I appreciate it, I will try it out at some point soon and get back to you.

from node-connect-pg-simple.

Related Issues (20)

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.