V
V
vladislav_nik2020-11-19 20:47:26
Sessions
vladislav_nik, 2020-11-19 20:47:26

Why session in Node.js is quickly reset?

The session is established after authorization. If you switch to links within the site, everything works fine. It is worth leaving the tab inactive, after 5-10 minutes the session is reset.
Session configuration code:

const session = require('express-session');

app.use(session({
    secret: 'you secret key',
    saveUninitialized: false,
    resave: false,
    cookie: {
        maxAge: 30 * 24 * 60 * 60 * 1000
    }
}));


I played with the saveUninitialized and resave parameters in different ways, tried to set true / false in different combinations. Did not help. During the lifetime of the session, the maxAge property should be responsible, I set it to 1 month, but it still lives for 5-10 minutes.

Perhaps now they will advise me as a session storage - Redis, but I want to know how to fix the error in this particular configuration, without using storages like Redis and MongoDB.

Tell me how to generate a key for the secret field.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FinGanapre, 2020-11-19
@FinGanapre

secret is passed through an environment variable. Your task is to create an .env file and set the necessary variables there (database connection, secret key, port, host...). To read a file, use dotenv . To do this, create a config folder, for example, in it the index.js file in which you write the exported object with the necessary config. You will use this object to access environment variables in other modules. When including the library, it is better to specify the path to the .env file in full (__dirname ...).
The .env file is not uploaded to the repository, specify it in .gitignore
If you are talking about generating a key as a sequence of characters, then any password generator is an option. More characters and let's go.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question