Session doesn't get saved to database
bschley opened this issue · comments
Hello.
my session gets not saved to the sqlite3 db. My db works, I can create users etc. and the sessions table gets also added to my db.
I implemented it like this:
Imports
import session from "express-session";
import SQLiteStore from "connect-sqlite3";
Implementation in my server.js
app.use(methodOverride());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(
session({
resave: false,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
cookie: { maxAge: 1000 * 60 * 60 * 24 },
store: new SQLiteStore(session)({
dir: "./db",
db: "main.db",
table: "sessions",
concurrentDB: true,
}),
})
);
What else can be the problem?
Ok, found the issue.
I had to do it like this:
import session from "express-session";
import SQLiteStore from "connect-sqlite3";
const Store = new SQLiteStore(session);
app.use(
session({
store: new Store({ dir: "./db", db: "main.db", table: "sessions" }),
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
cookie: { maxAge: 1000 * 60 * 60 * 24 },
})
);