Reputation: 1600
index.js
var store = new MongoDBStore({
uri:
"mongodb+srv://...........................?retryWrites=true&w=majority",
collection: "........."
});
app.use(cookieParser());
app.use(
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 3600000
},
store: store
})
);
account.ts
exports.postLogin = (req, res, next) => {
const email = req.body.email;
const password = req.body.password;
if (email == "[email protected]" && password == "1234") {
console.log("work is true");
req.session.isAuthenticated = "true";
}
};
Problem: Post added successfully. But the session is not working. There is no error message. How can I solve this problem?
Upvotes: 0
Views: 777
Reputation: 2081
you use this config in index.js
file, it work for me
const express = require('express')
const app = express()
const cookieParser = require('cookie-parser');
const session = require('express-session')
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo')(session);
mongoose.connect('mongodb://localhost/my-database', {
useMongoClient: true
});
mongoose.Promise = global.Promise;
const db = mongoose.connection
app.use(cookieParser());
app.use(session({
secret: 'my-secret',
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 1000 * 60 * 60 // 1 hour
}
store: new MongoStore({ mongooseConnection: db })
}));
app.use(passport.initialize());
// persistent login sessions
app.use(passport.session());
and you should make middle ware with passport
and work with it. and read this link
Upvotes: 2