Thursday, 11 May 2017

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?



via user7103883

No comments:

Post a Comment