Node.js + express.js + passport.js : stay authenticated between server restart

后端 未结 7 640
谎友^
谎友^ 2020-12-04 05:12

I use passport.js to handle auth on my nodejs + express.js application. I setup a LocalStrategy to take users from mongodb

My problems is that users have to

7条回答
  •  独厮守ぢ
    2020-12-04 05:57

    I'm using mongoose, I tried the code presented in the answers above and it didn't work for me. I got this error when I did:

    Error: db object already connecting, open cannot be called multiple times
    

    However, this works for me:

    app.use(express.session({
        secret:'secret',
        maxAge: new Date(Date.now() + 3600000),
        store: new MongoStore({mongoose_connection:mongoose.connection})
    }))
    

    Note: If you don't have MongoStore for whatever reason, you need to do:

    npm install connect-mongo --save
    

    then:

    var MongoStore = require('connect-mongo')(express)
    

提交回复
热议问题