express-session-resolver

    0.1.2 • Public • Published

    express-session-resolver

    This module makes you to get session-id from the cookie which the id is generated by express-session.

    Requirement

    Usecase

    Suppose that they have 2 same domain's servers, Socket IO's sever is able to authenticate reauests the by cookie which is generated from the express server. express-session-resolver convert cookie to session-id, and queries to MySQL server.
    usecase

    How to use

    npm install express-session-resolver

    then,

    const Resolver = require('express-session-resolver');
    const resolver = new Resolver({
        mysql: {
            host: "your my sql server's host",
            port: "the port",
            user: "the user",
            password: "the password",
            database: "the database name"
        },
        name: "cookie name", 
        secret: "secret"
    });
     
    // --------------------------------
    // For example, Socket IO's server
    // At 1st, you can use middleware for auth. 
    const authMiddleware = function() {
     
        return async function(socket, next) {
            
            const noCookies = !socket.handshake.headers || !socket.handshake.headers.cookie
     
            if (noCookies) {
                next(new Error('Auth error'))
                return
            }
     
            const session = await resolver
                .findSession(socket.handshake.headers.cookie)
                .catch(()=>null)
     
            if (session) {
                next()
            } else {
                next(new Error('Auth error (not match)'))
            }
        }
    }
    // add it as a middleware 
    io.use(authMiddleware())
     
    // --------------------------------
    // For example, express server
    app.get("/secret-page", function(req, res, next){
        resolver.findSession(req.headers.cookie)
        .then(result=>{
            res.send(result[0].session_id);
        })
    })

    Install

    npm i express-session-resolver

    DownloadsWeekly Downloads

    1

    Version

    0.1.2

    License

    MIT

    Unpacked Size

    42.6 kB

    Total Files

    11

    Last publish

    Collaborators

    • avatar