Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 941
user7103883 Avatar asked Oct 17 '22 12:10

user7103883


1 Answers

  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
like image 57
Aule Avatar answered Oct 21 '22 00:10

Aule