The simple example on https://github.com/voxpelli/node-connect-pg-simple shows:
var session = require('express-session');
app.use(session({
store: new (require('connect-pg-simple')(session))(),
secret: process.env.FOO_COOKIE_SECRET,
resave: false,
cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 } // 30 days
}));
But when I try it, node complains:
throw new Error('No database connecting details provided to
connect-pg-simple');
How do I specify the connection string?
// I was stuck with the same proble and solved it
// 1-) Connection details
const conObject = {
user: 'mehmood',
password: 'mehmood',
host: 'localhost',// or whatever it may be
port: 5432,
database: 'test_db'
};
// 2-) Create an instance of connect-pg-simple and pass it session
const pgSession = require('connect-pg-simple')(session);
// 3-) Create a config option for store
const pgStoreConfig = {
pgPromise: require('pg-promise')({ promiseLib: require('bluebird') })({
conObject }), // user either this
//conString: 'postgres://mehmood:mehmood#localhost:5432/test_db', // or this
// conObject: conObject,// or this,
// pool: new (require('pg').Pool({ /* pool options here*/}))// or this
}
// 4-) use the store configuration to pgSession instance
app.use(session({
store: new pgSession(pgStoreConfig),
secret: 'jW8aor76jpPX', // session secret
resave: true,
saveUninitialized: true,
cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 } // 30 days
}));
// Share improve this answer
For me it worked like
conString:'postgres://postgres:password123#localhost:5432/edu';
So the format must be
conString:'postgres://<user>:<database_password>#<hostname>:<port>/<database_name';
For deploying you get a username. If you run on local machine then PostgreSQL
Related
I'm using the following to configure my session on subdomain1.example.com:
const expressInstance = express();
expressInstance.use(
session({
secret: "my secret",
cookie: {
domain: '.example.com',
sameSite: 'none',
secure: true,
maxAge: 1000 * 60 * 60 * 48
}
})
);
expressInstance.set('trust proxy', 1);
Then I set it as:
res.cookie("cookie_name", "cookie_value")
I can see this cookie when I visit subdomain1.example.com but not when I visit example.com.
What am I missing? Isn't this a very common use case?
In my NodeJs / Express app, I'm using the standard session package and Passport to handle sessions and login. My problem is that the app kicks the user out after what feels like 10 minutes of inactiviy, and forces them to log-in again. My assumption is that it must be something to do with the session configuration, which with my limited understanding, I think is configured to allow 2 hours:
const session = require("express-session");
const PostgreSqlStore = require("connect-pg-simple")(session);
const sessionAge = 2 * 60 * 60 * 1000; // hour, min, sec, millisecond
var sessionConfig = {
name: "mysite",
secret: "verysecret",
resave: true,
saveUninitialized: false,
proxy: trustedTypes,
cookie: {
key: "cookieKey",
secure: true,
sameSite: false,
httpOnly: true,
maxAge: sessionAge,
},
store: new PostgreSqlStore({
pgPromise: db,
ttl: 2 * 60 * 60, //Hours, minute, seconds
}),
};
app.use(session(sessionConfig));
Is there anything I'm doing wrong, or is there something else I should be looking at to find the cause of this behavior?
store: new PostgreSqlStore({
pgPromise: db,
ttl: 2 * 60 * 60, //Hours, minute, seconds
})
I think your PostgreSQL store ttl property should be equivalent to maxAge property of session config
I've discovered that apparently although by default the session does get extended on the server, it won't send an updated cookie to the browser if nothing has changed in it. The missing property is the 'rolling' attribute.
var sessionConfig = {
rolling: true,
Hello I am fairly new to connect-mongodb-session library so I would love to know how I can access the collection of user sessions so that I can have a code logic to check if user session has expired so that I can remove it from collection.
I know with connect-mongo:
I can set it up like this below to check each hour for expired sessions and it automatically removes the expired sessions
const sessionStore = new MongoStore({
db: 'myappsession',
clear_interval: 3600
});
But now with connect-mongodb-session there's no prop 'clear_interval'
This is how my connect-mongodb-session store is made:
const mongoStore = MongoStore(expressSession);
const store = new mongoStore({
collection: "userSessions",
uri: process.env.mongoURI,
expires: 10 * 1000 * 24 * 60 * 60,
});
So when things are like this how can I go about this
var store=mongoDbStore({
uri:connectionString,
collection:"mySessions"
})
app.use(
session({
secret:key,
resave: true,
saveUninitialized:false,
cookie:{
maxAge:3600000
},
store:store
})
)
You can give a maxAge like above. This session will be terminated.
I'm trying insert my data (like login or id) into a session.
But when I'm writing req.session.login, typescript returns error:
"Object is possibly 'undefined'." I was trying everything and I was looking for answer in google, but I didn't found anything.
Here is my 'src/app.ts' included into simple start file 'bin/www.js'.
import * as express from "express";
import * as session from 'express-session';
import * as cookieParser from "cookie-parser";
let app:express.Application = express();
app.use(cookieParser("sssh!"));
app.use(session({
secret: 'sssssh! That\'s the secret',
name: 'session',
resave: false,
saveUninitialized: true,
cookie: {
path: '*',
maxAge: 60 * 60 * 1000,
expires: new Date(Date.now() + (3 * 60 * 60 * 1000))
}
}));
app.use("/test", (req:express.Request, res:express.Response, next:express.NextFunction)=>{
let ses = req.session;
// Here I want to do something similar to cookies,
//like req.session.login = "login" or ses.login = "login"
res.cookie('name', "lastname");
console.log(req.cookies);
res.send(req.session);
});
If I'm not wrong, this declaration (req.session.login = "login") should work in normal javascript file.
I'm sure that I missed something important here.
I am trying to get persistent sessions working using RedisStore but whenever i close the browser the session is destroyed. I tried setting the maxAge and ttl but nothing makes the session survive a browser restart. What am i doing wrong?
The relevant parts of my code are:
var app = express();
var RedisStore = require('connect-redis')(express);
app.use(express.cookieParser('my secret key'));
app.use(express.session({
store: new RedisStore({
host: 'localhost',
port: 6379,
ttl: (60000 * 24 * 30),
cookie: { maxAge: (60000 * 24 * 30) }
}),
secret: 'my secret key'
}));
The cookie should belong in the object you pass to express.session, not in the object you pass to RedisStore.
Try the following:
app.use(express.session({
store: new RedisStore({ host: 'localhost', port: 6379, ttl: (60000 * 24 * 30)}),
cookie: { maxAge: (60000 * 24 * 30)},
secret: 'my secret key'
}));