Skip to content

Commit

Permalink
Merge pull request #42 from korac/feat/support-cookie-subdomains
Browse files Browse the repository at this point in the history
[feat] Support cookie subdomains
  • Loading branch information
BJvdA authored Feb 1, 2022
2 parents ae94db6 + ac384e2 commit 15ba0ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/api/__tests__/loginHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ describe('[api] loginHandler', () => {
jest.restoreAllMocks();
});

it('should set the domain if configured', async () => {
const { req, res } = createMocks(
{ method: 'POST', body: { password: 'password' } },
{ eventEmitter: EventEmitter },
);

const domain = 'storyofams.com';
await loginHandler('password', { domain })(req, res);

expect(res._getStatusCode()).toBe(200);
expect(res._getHeaders()).toMatchObject({
'set-cookie': expect.stringMatching(
new RegExp(
`^next-password-protect=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9\\..+\\..+; Domain=${domain}; Path=\\/; HttpOnly$`,
),
),
});
});

it('should reject on incorrect password', async () => {
const { req, res } = createMocks(
{ method: 'POST', body: { password: 'incorrect' } },
Expand Down
2 changes: 2 additions & 0 deletions src/api/loginHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface PasswordProtectHandlerOptions {
cookieName?: string;
cookieSameSite?: boolean | 'lax' | 'none' | 'strict';
cookieSecure?: boolean;
domain?: string;
}

export const loginHandler = (
Expand Down Expand Up @@ -40,6 +41,7 @@ export const loginHandler = (
*/
jwt.sign({}, password),
{
domain: options?.domain,
httpOnly: true,
sameSite: options?.cookieSameSite || false,
secure:
Expand Down

1 comment on commit 15ba0ac

@vercel
Copy link

@vercel vercel bot commented on 15ba0ac Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.