lsp

laravel-session-parser

Validate laravel 5 session in Node.js

Showing:

Popularity

Downloads/wk

0

GitHub Stars

4

Maintenance

Last Commit

6yrs ago

Contributors

0

Package

Dependencies

2

License

ISC

Type Definitions

Tree-Shakeable

No?

Categories

Readme

laravel-session-parser

Validate laravel 5 session in Node.js

Config your laravel app to store session in mysql or redis

Install

npm install laravel-session-parser

Usage

var lsp = require('laravel-session-parser');

Examples:

mysql exapmle
var SECRET_KEY = 'LARAVEL_KEY';

io.use(function(socket, next) {

    var cookies = cookie.parse(socket.handshake.headers.cookie);
    var laravelSession = cookies.laravel_session;
    var sessionId = lsp.decryptSession(laravelSession, SECRET_KEY);
    // function return session from db sessions table, see example bellow
    getSessionMysql(sessionId, function(err, session) {
        if (!err && session) {
            lsp.getUserIdFromSessionIdMysql(session, function(u_id) {
                if (u_id) {
                    return next();
                };
                return next(new Error('Authentication error'));
            })
        } else {
            return next(new Error('Authentication error'));
        };
    })
});
redis exapmle
var SECRET_KEY = 'LARAVEL_KEY';

io.use(function(socket, next) {

    var cookies = cookie.parse(socket.handshake.headers.cookie);
    var laravelSession = cookies.laravel_session;
    var sessionId = lsp.decryptSession(laravelSession, SECRET_KEY);
    // function return session from db sessions table, see example bellow
    getSessionRedis(sessionId, function(err, session){
        if (!err && session) {
            lsp.getUserIdFromSessionIdRedis(session, function(u_id) {
                if (u_id) {
                    return next();
                };
                return next(new Error('Authentication error'));
            })
        } else {
            return next(new Error('Authentication error'));
        };
    })
});
// just example, you can use your own
function getSessionMysql (s_id, cb) {
    mysql.query('SELECT payload from `sessions` WHERE `id`= "' + s_id + '"', function(err, rows, fields) {
        if (err) {
            cb && cb (err);
            return;
        };
        if (rows.length > 0) {
            cb && cb (null, rows[0].payload);
        } else {
            cb && cb (null, false);
        };
    });
}

function getSessionRedis (s_id, cb) {
    var _sessionId = 'laravel:' + s_id;
    client.get(_sessionId, function(err, session) {
        if (err) {
            cb && cb (err);
            return;
        };
        cb && cb (null, session);
    });
}

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial