Skip to content

Commit

Permalink
Use decorateReply (fixes #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Sep 10, 2017
1 parent c868c89 commit 4baddb2
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const fp = require('fastify-plugin')
const cookie = require('cookie')

function plugin (fastify, options, next) {
fastify.decorateReply('setCookie', function (name, value, options) {
const seriaized = cookie.serialize(name, value, options || {})
this.header('Set-Cookie', seriaized)
return this
})

fastify.addHook('preHandler', (fastifyReq, fastifyRes, done) => {
if (fastifyReq.cookies) done()
if (!fastifyReq.req.headers) fastifyReq.req.headers = {}
Expand All @@ -12,12 +18,6 @@ function plugin (fastify, options, next) {
const cookies = (cookieHeader) ? cookie.parse(cookieHeader) : []
fastifyReq.cookies = cookies

fastifyRes.setCookie = function (name, value, options) {
const seriaized = cookie.serialize(name, value, options || {})
this.header('Set-Cookie', seriaized)
return this
}

done()
})

Expand Down

0 comments on commit 4baddb2

Please sign in to comment.