Skip to content

Commit

Permalink
fix(server): fix setting TTL index on collection : sessions error
Browse files Browse the repository at this point in the history
  • Loading branch information
DaftMonk committed Jul 28, 2014
1 parent a6c3f1a commit 0581ed0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
4 changes: 2 additions & 2 deletions app/templates/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ module.exports = function (grunt) {
setTimeout(function () {
grunt.log.writeln('Done waiting!');
done();
}, 500);
}, 1500);
});

grunt.registerTask('express-keepalive', 'Keep grunt running', function() {
Expand All @@ -684,7 +684,7 @@ module.exports = function (grunt) {

grunt.registerTask('serve', function (target) {
if (target === 'dist') {
return grunt.task.run(['build', 'env:all', 'env:prod', 'express:prod', 'open', 'express-keepalive']);
return grunt.task.run(['build', 'env:all', 'env:prod', 'express:prod', 'wait', 'open', 'express-keepalive']);
}

if (target === 'debug') {
Expand Down
10 changes: 8 additions & 2 deletions app/templates/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ var socketio = require('socket.io').listen(server);
require('./config/socketio')(socketio);<% } %>
require('./config/express')(app);
require('./routes')(app);

<% if (filters.twitterAuth) { %>
mongoose.connection.on('connected', function () {
// Start server
server.listen(config.port, config.ip, function() {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
});<% } else { %>
// Start server
server.listen(config.port, config.ip, function () {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
});<% } %>

// Expose app
exports = module.exports = app;
26 changes: 12 additions & 14 deletions app/templates/server/config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var path = require('path');
var config = require('./environment');<% if (filters.auth) { %>
var passport = require('passport');<% } %><% if (filters.twitterAuth) { %>
var session = require('express-session');
var mongoStore = require('connect-mongo')(session);<% } %>
var mongoStore = require('connect-mongo')(session);
var mongoose = require('mongoose');<% } %>

module.exports = function(app) {
var env = app.get('env');
Expand All @@ -32,19 +33,16 @@ module.exports = function(app) {
app.use(cookieParser());
<% if (filters.auth) { %>app.use(passport.initialize());<% } %><% if (filters.twitterAuth) { %>

// Persist sessions with mongoStore
// We need to enable sessions for passport twitter because its an oauth 1.0 strategy
app.use(session({
secret: config.secrets.session,
resave: true,
saveUninitialized: true,
store: new mongoStore({
url: config.mongo.uri,
collection: 'sessions'
}, function () {
console.log('db connection open' );
})
}));<% } %>
mongoose.connection.on('connected', function () {

This comment has been minimized.

Copy link
@meeDamian

meeDamian Jul 30, 2014

Contributor

running this code in mongoose callback breaks Twitter authentication with:

Error: OAuthStrategy requires session support. Did you forget app.use(express.session(...))?
    at Strategy.OAuthStrategy.authenticate (/home/mee/p/my-awesome-project/node_modules/passport-twitter/node_modules/passport-oauth1/lib/strategy.js:120:41)
    at Strategy.authenticate (/home/mee/p/my-awesome-project/node_modules/passport-twitter/lib/strategy.js:85:40)
    at attempt (/home/mee/p/my-awesome-project/node_modules/passport/lib/middleware/authenticate.js:337:16)
    at Object.authenticate [as handle] (/home/mee/p/my-awesome-project/node_modules/passport/lib/middleware/authenticate.js:338:7)
    at next_layer (/home/mee/p/my-awesome-project/node_modules/express/lib/router/route.js:103:13)
    at Route.dispatch (/home/mee/p/my-awesome-project/node_modules/express/lib/router/route.js:107:5)
    at c (/home/mee/p/my-awesome-project/node_modules/express/lib/router/index.js:195:24)
    at Function.proto.process_params (/home/mee/p/my-awesome-project/node_modules/express/lib/router/index.js:251:12)
    at next (/home/mee/p/my-awesome-project/node_modules/express/lib/router/index.js:189:19)
    at Function.proto.handle (/home/mee/p/my-awesome-project/node_modules/express/lib/router/index.js:234:5)

This comment has been minimized.

Copy link
@DaftMonk

DaftMonk Jul 31, 2014

Author Member

You're right. I just stopped getting the TTL errors in the project I was testing by using this but didn't actually trigger the twitter callback.

I reverted this change, but that TTL error can still be triggered when using sessions, and I'm not sure how to solve it once and for all.

This comment has been minimized.

Copy link
@meeDamian

meeDamian Jul 31, 2014

Contributor

Could you point me to the exact problem you were trying to fix?

This comment has been minimized.

Copy link
@DaftMonk

DaftMonk Jul 31, 2014

Author Member

It's inconsistent and hard to reproduce, but this is the error jdesboeufs/connect-mongo#65

// Persist sessions with mongoStore
// We need to enable sessions for passport twitter because its an oauth 1.0 strategy
app.use(session({
secret: config.secrets.session,
resave: true,
saveUninitialized: true,
store: new mongoStore({ mongoose_connection: mongoose.connection })
}));
});<% } %>

if ('production' === env) {
app.use(favicon(path.join(config.root, 'public', 'favicon.ico')));
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"passport-local": "~0.1.6",
"passport-facebook": "latest",
"passport-twitter": "latest",
"passport-google-oauth": "latest",
"composable-middleware": "^0.3.0",
"connect-mongo": "^0.4.1",
"socket.io": "~0.9.16",
Expand Down
4 changes: 2 additions & 2 deletions test/test-file-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('angular-fullstack generator', function () {
// });
});

describe('with other preprocessors', function() {
describe('with other preprocessors and oauth', function() {
beforeEach(function() {
helpers.mockPrompt(gen, {
script: 'coffee',
Expand All @@ -166,7 +166,7 @@ describe('angular-fullstack generator', function () {
router: 'uirouter',
mongoose: true,
auth: true,
oauth: [],
oauth: ['twitterAuth', 'facebookAuth', 'googleAuth'],
socketio: true
});
});
Expand Down

0 comments on commit 0581ed0

Please sign in to comment.