passport-socketio-redis Library for socket.io 1.x and express.js 4.x
Share user information from a passport.js stored in Connect-Redis with socket.io connection.
Installation
$ npm install passport-socketio-redis
Example usage
// Create http server (Based on express.js) var express = ;var app = ;var server = app;var cookieParser = ;var redis = ;var passport = ;var session = ;var RedisStore = session; var socketioRedis = ; // When configure your session for express use options like this.app
passport-socketio-redis - Options
cookieParser
[function] required:
You have to provide your cookieParser from cookie-parser: express.cookieParser
store
[function] required:
You have to provide connect-redis as a SessionStore
key
[string] optional:
Defaults to 'connect.sid'
. But you're always better of to be sure and set your own key. Don't forget to also change it in your express.session()
:
app.use(session({ key: 'your.sid-key' }));
secret
[string] optional:
As with key
, also the secret you provide is optional. But: be sure to have one. That's always safer. You can set it like the key:
app.use(session({ secret: 'pinkie ate my cupcakes!' }));
passport
[function] optional:
Defaults to require('passport')
. If you want, you can provide your own instance of passport for whatever reason.
socket.request.user
This property is always available from inside a io.on('connection')
handler. If the user is authorized via passport, you can access all the properties from there.
for example:
io
Plus you have the socket.request.user.logged_in
property which tells you whether the user is currently authorized or not.
Additional methods
passportSocketIo.filterSocketsByUser
This function gives you the ability to filter all connected sockets via a user property. Needs two parameters function(io, function(user))
. Example:
passportSocketIo;
Clientside:
You have to provide the session-cookie. If you haven't set a name yet, do it like this: app.use(session({ key: 'your.sid-key' }));
For example, my Angular.js implementation
appServices;
Serverside:
Nope, there's nothing to do on the server side. Just be sure that the cookies names match.
License
Licensed under the MIT-License. 2012-2015 Filip Lukac.