Skip to content

Commit

Permalink
fix(handler): handle case where no cookie is present in headers
Browse files Browse the repository at this point in the history
This is actually the case in 100% of new users of this library, so kind of a
problem to have this bug :D.

fixes #35
  • Loading branch information
vvo committed Mar 30, 2020
1 parent ae1dd6a commit 4dcc7b8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
8 changes: 4 additions & 4 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3778,10 +3778,10 @@ neo-async@^2.5.0, neo-async@^2.6.0, neo-async@^2.6.1:
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.1.tgz#ac27ada66167fa8849a6addd837f6b189ad2081c"
integrity sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw==

next-iron-session@latest:
version "3.0.2"
resolved "https://registry.yarnpkg.com/next-iron-session/-/next-iron-session-3.0.2.tgz#6cfa8e856ebbce09d4b90229fdd420916ad261f0"
integrity sha512-EdjrWU9Kvdy+ekmTh5NNJM8w7tqq/6nlRvXaHPPIr7fU4WQrFvkWddyG0ET9Ph+DxtU6h++p4X1DiZBzNCBdcQ==
next-iron-session@3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/next-iron-session/-/next-iron-session-3.1.0.tgz#79469e28f48b895665e10defac829d6027c89d3d"
integrity sha512-WHNd/CW1E6t6nFQ/isggrriC7//N+hgZrLOElEln7+yMdfAkBmSKHOwrb2rCWg51wbRvgebUlF7P3WIoDBxtNQ==
dependencies:
clone "^2.1.2"
cookie "^0.4.0"
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default function withIronSession(
const res = handlerType === "api" ? args[1] : args[0].res;

const store = await getOrCreateStore({
sealed: cookie.parse(req.headers.cookie)[cookieName],
sealed: cookie.parse(req.headers.cookie || "")[cookieName],
password,
ttl: ttl * 1000,
});
Expand Down
20 changes: 20 additions & 0 deletions lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,23 @@ test("It throws Iron errors when passing a wrong password", async () => {
`"Password string too short (min 32 characters required)"`,
);
});

test("when no cookies at all", () => {
return new Promise((done) => {
const handler = (req) => {
expect(req.session.set("user", { id: 20 })).toMatchInlineSnapshot(`
Object {
"id": 20,
}
`);
done();
};
const wrappedHandler = withIronSession(handler, { password });
wrappedHandler(
{
headers: {},
},
{},
);
});
});

0 comments on commit 4dcc7b8

Please sign in to comment.