I want to create a user sessison when user enter the app. And read the session whenever needed. Here is my try
var io = require('socket.io'),
express = require('express');
querystring = require('querystring');
var app = express.createServer();
app.get('/', function(req, res){
var sessionVal = querystring.parse(req.url.substr(2));// sessionVal is an email for example: [email protected]
app.use(express.cookieParser());
app.use(express.session({ secret: sessionVal }));
});
var socket = io.listen(app);
socket.on('connection', function(client) {
client.on('message', function(message) {
// message will be an object {text:'user text chat blah blah', email:'[email protected]'}
// if the seesion stored, has the same value with message.email
// then the message will be broadcasted
socket.broadcast(message.text);
// else will not broadcast
});
});
app.listen(4000);
This question is related to
node.js
Steps I did:
Init cookies as being NOT http-only in server-side app.'s:
app.configure(function(){
//a bunch of stuff
app.use(express.cookieSession({secret: 'mySecret', store: store, cookie: cookieSettings}));```
Then in client-side services.jss I put ['ngCookies'] in like this:
angular.module('swrp', ['ngCookies']).//etc
Then in controller.js
, in my function UserLoginCtrl
, I have $cookies
in there with $scope
at the top like so:
function UserLoginCtrl($scope, $cookies, socket) {
Lastly, to get the value of a cookie inside the controller function I did:
var mySession = $cookies['connect.sess'];
Now you can send that back to the server from the client. Awesome. Wish they would've put this in the Angular.js documentation. I figured it out by just reading the actual code for angular-cookies.js directly.
I forgot to tell a bug when i use I use req.session.email = req.param('email'), the server error says cannot sett property email of undefined.
The reason of this error is a wrong order of app.use. You must configure express in this order:
app.use(express.cookieParser());
app.use(express.session({ secret: sessionVal }));
app.use(app.route);
Hello I am trying to add new session values in node js like
req.session.portal = false
Passport.authenticate('facebook', (req, res, next) => {
next()
})(req, res, next)
On passport strategies I am not getting portal value in mozilla request but working fine with chrome and opera
FacebookStrategy: new PassportFacebook.Strategy({
clientID: Configuration.SocialChannel.Facebook.AppId,
clientSecret: Configuration.SocialChannel.Facebook.AppSecret,
callbackURL: Configuration.SocialChannel.Facebook.CallbackURL,
profileFields: Configuration.SocialChannel.Facebook.Fields,
scope: Configuration.SocialChannel.Facebook.Scope,
passReqToCallback: true
}, (req, accessToken, refreshToken, profile, done) => {
console.log(JSON.stringify(req.session));
It is cumbersome to interoperate socket.io and connect sessions support. The problem is not because socket.io "hijacks" request somehow, but because certain socket.io transports (I think flashsockets) don't support cookies. I could be wrong with cookies, but my approach is the following:
Source: Stackoverflow.com