how do you refresh the session cookie expiration?
- Dominant language
- No language data
- Stars
- 68
- Forks
- 8
- Avg merge
- 11h 2m
- Merged PRs (30d)
- 2
Description
I'm trying to handle sessions using fastify-session.
I have a postgres database where I persist the session. This is my configuration in app.js
```
await app.register(session, {
secret: 'mysecret',
saveUninitialized: false,
cookieName: 'sessionId',
cookie: {
httpOnly: true,
maxAge: 1000*60,
secure: process.env.NODE_ENV == 'development'
},
store: new knexSession({
knex: app.db
})
});
```
I have a login route that handles user's login (duh):
```
async function loginHandler(req, reply) {
if (req.session.user != undefined) {
return reply.badRequest('Already logged in');
}
const { username, password } = req.body;
const user = await this.db('useraccount')
.where('username', username)
.first();
if (!user || !await bcrypt.compare(password, user.password))
return reply.badRequest('Invalid username/password');
req.session.user = { userId: user.id, role: user.role };
return await Serializer.serializeAsync("useraccount", user);
}
```
When a user is logged in, I want to refresh the expiration of the session cookie so that the expiration is maxAge from the last activity he performed.
It seems that session.touch is what I need, but it looks like it works sometimes and not others.
I'm using fastify-auth on protected routes as follows
```
await app.decorate('verifySessionCookie', async (req, reply) => {
try {
const { userId } = req.session.user;
await req.session.touch(); // <<<<<<<<<<<<<---------------- is this correct?
} catch (e) {
return reply.unauthorized('Must be logged in');
}
})
fastify.get('/me', {
preHandler: fastify.auth([
fastify.verifySessionCookie
]),
}, getMeHandler);
```
Is using **await req.session.touch()**; correct?
Contributor guide
Assessment
This issue has not been assessed yet.