5

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?

Community
  • 1
  • 1
user7103883
  • 87
  • 1
  • 7

1 Answers1

3
  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
Community
  • 1
  • 1
Aule
  • 650
  • 5
  • 13
  • 2
    I think we wanted to know how to access those database-stored sessions –  Apr 08 '20 at 19:35