I am attempting to add authentication to my node.js/express.js/react.js
application.
My directory structure is as follows:
application
server.js
www
js
app.js
sessions.js
I add these two lines to server.js
var express = require('express'),
app = express(),
path = require('path'),
http = require('http').Server(app),
io = req
let sessions = require('./www/js/sessions');
app.use(sessions);
I run node server.js
I see a new message I have not seen before:
$ node server.js
express-session deprecated req.secret; provide secret option www/js/sessions.js:16:22
and then in the browser when I connect to I get this error. If I remove the two lines above from server.js
the program runs fine:
In the browser, I see exception:
TypeError: passport.initialize is not a function
at module.exports.passport (/home/idf/Documents/js/react-trader/www/js/sessions.js:18:56)
at Layer.handle [as handle_request] (/home/idf/Documents/js/react-trader/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/idf/Documents/js/react-trader/node_modules/express/lib/router/index.js:312:13)
at /home/idf/Documents/js/react-trader/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/idf/Documents/js/react-trader/node_modules/express/lib/router/index.js:330:12)
at next (/home/idf/Documents/js/react-trader/node_modules/express/lib/router/index.js:271:10)
at expressInit (/home/idf/Documents/js/react-trader/node_modules/express/lib/middleware/init.js:33:5)
at Layer.handle [as handle_request] (/home/idf/Documents/js/react-trader/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/idf/Documents/js/react-trader/node_modules/express/lib/router/index.js:312:13)
at /home/idf/Documents/js/react-trader/node_modules/express/lib/router/index.js:280:7
I can't tell from the error if it is complaining that it can't find sessions.js
, or if it is failing to find an npm
requirement. I did install express-session
and I see it in the package.json
so not sure...
{
"name": "TraderWS",
"version": "1.0.0",
"description": "Trader Workstation",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Me",
"license": "MIT",
"dependencies": {
"babel-loader": "^6.2.10",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.22.0",
"body-parser": "^1.16.1",
"connect-redis": "^3.2.0",
"express": "^4.12.3",
"express-session": "^1.15.1",
"griddle-react": "^0.7.1",
"react": "^15.4.2",
"rx": "^4.0.7",
"socket.io": "^1.3.5",
"webpack": "^2.2.1"
}
}
This may or may not help anyone else.
Initially the problem was that I was not passing a passport to sessions. This fixed that problem:
let usersService = require('./service/users');
let bodyParser = require('./middleware/bodyParser');
let passport = require('./config/passport');
let sessions = require('./middleware/sessions')(passport); // was missing parameter passport
Then I had another problem in that I had code below with the commented out version, and that caused an error because there was no secret. Once I actually entered a string secret, the problem went away
'use strict';
const session = require('express-session');
let config = {
//secret: process.env.SESSION_SECRET,
secret: 'keyboard cat',
saveUninitialized: false,
resave: false
};
if (process.env.REDIS_URL) {
const RedisStore = require('connect-redis')(session);
config.store = new RedisStore({ url: process.env.REDIS_URL });
}
let expressSession = session(config);
module.exports = passport => [expressSession, passport.initialize(), passport.session()];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.