Skip to content

Instantly share code, notes, and snippets.

@AlexSKuznetsov
Last active June 24, 2020 05:54
Show Gist options
  • Select an option

  • Save AlexSKuznetsov/772da3682a011e4b4eb7f17967150149 to your computer and use it in GitHub Desktop.

Select an option

Save AlexSKuznetsov/772da3682a011e4b4eb7f17967150149 to your computer and use it in GitHub Desktop.
// Установка
npm install express-session
const session = require('express-session');
// MiddleWare для сессий
app.use(session({
secret: 'keyboard cat', // это нужно изменить на свой рандомный ключь
resave: false,
saveUninitialized: false, // это нужно для инициализации сессии сразу
cookie: { secret: false }, // // значение true для https сервера
}))
// Куки парсер
app.use(cookieParser());
// Удаление сессии при логауте
route.get('/:name', (req, res) => {
req.session.destroy(() => {
res.clearCookie('connect.sid', { path: '/' });
res.redirect('/entries');
});
})
// Middleware для записи в данных из сессии в локалс
app.use((req, res, next) => {
if (req.session.name) {
res.locals.name = req.session.name;
res.locals.email = req.session.email;
}
next();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment