简体   繁体   中英

How to use sessions with connect-mongo?

In docs of connect-mongo I read only about it set up, nothing more. How to define sessions? How to read?

const mongoose = require("mongoose");
mongoose.Promise = Promise;
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);

mongoose.connect('mongodb://localhost/MYDATABASE');

app.use(session({
    secret: "SOME_SECRET_KEY",
    store: new MongoStore({ mongooseConnection: mongoose.connection })
}));

Okay, I set up. If I have

app.get("/login", function(req, res){
// If user authorized
// I want to define a session.user = req.body.user 
// And then I want to read this value in other my site pages
});

How I can define user login and some other data to session?

How I can read this values?

Where this session will store in MongoDB? Or I need to define not only way to MYDATABASE and to MYDATABASE/sessionstore ?

Must I to generate secret or this must be a one defined string?

  1. Reading from and writing to the session is done through the req.session object: req.session.userId = req.body.userId

  2. The session data will be stored in a collection called sessions by default.

  3. About the session secret

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM