Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function fastifySession (fastify, options, next) {
if (!saveSession || isInsecureConnection) {
// if a session cookie is set, but has a different ID, clear it
if (cookieSessionId && cookieSessionId !== session.encryptedSessionId) {
reply.clearCookie(cookieName, { domain: cookieOpts.domain })
reply.clearCookie(cookieName, { domain: cookieOpts.domain, path: cookieOpts.path || '/' })
}

if (session.isSaved()) {
Expand Down
23 changes: 23 additions & 0 deletions test/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,29 @@ test("clearing cookie sets the domain if it's specified in the cookie options",
t.assert.strictEqual(response.headers['set-cookie'], 'sessionId=; Max-Age=0; Domain=domain.test; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax')
})

test("clearing cookie sets the path if it's specified in the cookie options", async t => {
t.plan(2)
const fastify = Fastify({ trustProxy: true })
await fastify.register(fastifyCookie)
await fastify.register(fastifySession, {
...DEFAULT_OPTIONS,
cookie: { path: '/admin' }
})
fastify.get('/admin', (_request, reply) => {
reply.send(200)
})
await fastify.listen({ port: 0 })
t.after(() => fastify.close())

const response = await fastify.inject({
url: '/admin',
headers: { cookie: DEFAULT_COOKIE_VALUE }
})

t.assert.strictEqual(response.statusCode, 200)
t.assert.strictEqual(response.headers['set-cookie'], 'sessionId=; Max-Age=0; Path=/admin; Expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax')
})

test('does not clear cookie if no session cookie in request', async t => {
t.plan(2)
const fastify = await buildFastify((_request, reply) => {
Expand Down
Loading