javascript - Node.JS + Passport.SocketIO: Edit And Save `socket.handshake.user` Properties -
i using node.js (0.10.28), passport.js (0.2.0) + passport-google (0.3.0), , passport.socketio (3.0.1).
currently, able access user created passport.js in app's paths using req.user
:
app.get('/profile', function(req, res) { // send user data res.send(req.user); });
using passport.socketio, able access user in:
io.sockets.on('connection', function(socket) { // user data console.log(socket.handshake.user); //... });
it possible edit req.user
, 'save' using req._passport.session.user.property = new_property_value
in app.get/post/all(...)
scope. updates show in io.sockets.on(...)
user object.
my question is: possible edit , 'save' socket.handshake.user
in io.sockets.on(...)
scope updated user show changes in req.user
in app.get/post/all(...)
? have tried following no avail:
io.sockets.on('connection', function(socket) { // rename username socket.handshake.user.username = 'new_username'; //... }); ... app.get('/profile', function(req, res) { // send user data res.send(req.user); // returns {..., username: 'old_username', ...} });
use socket.io-sessions (written same author made passport.socketio) change socket.handshake.user
in io.sockets.on(...)
.
code should this:
// initialization ... // ... io.sockets.on('connection', function(socket) { socket.handshake.getsession(function (err, session) { // socket.handshake.user session.passport.user socket.on(...) { ... } // .... // test username change session.passport.user.username = 'foobar'; // save session // note can call anywhere in session scope socket.handshake.savesession(session, function (err) { if (err) { // error saving! console.log('error saving: ', err); process.exit(1); } }); }); }); //... app.get('/profile', function(req, res) { // send user data res.send(req.user); // returns {..., username: 'foobar', ...} });
Comments
Post a Comment