-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve hoc, logouthandler
- Loading branch information
Showing
10 changed files
with
163 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ class MyApp extends App { | |
export default process.env.PASSWORD_PROTECT | ||
? withPasswordProtect(MyApp, { | ||
loginComponentProps: { | ||
backUrl: 'https://github.com/storyofams/next-password-protect', | ||
logo: 'https://storyofams.com/public/[email protected]', | ||
}, | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { logoutHandler } from '@storyofams/next-password-protect'; | ||
|
||
export default logoutHandler({ | ||
cookieName: 'authorization', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { EventEmitter } from 'events'; | ||
import { createMocks } from 'node-mocks-http'; | ||
|
||
import { logoutHandler } from '../logoutHandler'; | ||
|
||
describe('[api] logoutHandler', () => { | ||
it('should set max age on cookie to now to clear', async () => { | ||
const { req, res } = createMocks( | ||
{ method: 'POST', body: { password: 'password' } }, | ||
{ eventEmitter: EventEmitter }, | ||
); | ||
|
||
const maxAge = 0; | ||
const now = Date.now(); | ||
|
||
await logoutHandler()(req as any, res as any); | ||
|
||
expect(res._getStatusCode()).toBe(200); | ||
expect(res._getHeaders()).toHaveProperty( | ||
'set-cookie', | ||
`next-password-protect=; Max-Age=${maxAge}; Path=/; Expires=${new Date( | ||
now + maxAge, | ||
).toUTCString()}; HttpOnly`, | ||
); | ||
|
||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should handle secure option', async () => { | ||
const { req, res } = createMocks( | ||
{ method: 'POST', body: { password: 'password' } }, | ||
{ eventEmitter: EventEmitter }, | ||
); | ||
|
||
const maxAge = 0; | ||
const now = Date.now(); | ||
|
||
await logoutHandler({ cookieSecure: true })(req as any, res as any); | ||
|
||
expect(res._getStatusCode()).toBe(200); | ||
expect(res._getHeaders()).toHaveProperty( | ||
'set-cookie', | ||
`next-password-protect=; Max-Age=${maxAge}; Path=/; Expires=${new Date( | ||
now + maxAge, | ||
).toUTCString()}; HttpOnly; Secure`, | ||
); | ||
|
||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should throw on incorrect method', async () => { | ||
const { req, res } = createMocks( | ||
{ method: 'GET' }, | ||
{ eventEmitter: EventEmitter }, | ||
); | ||
|
||
await logoutHandler()(req as any, res as any); | ||
|
||
expect(res._getStatusCode()).toBe(500); | ||
|
||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should gracefully error', async () => { | ||
const { req, res } = createMocks( | ||
{ method: 'POST', body: { password: 'password' } }, | ||
{ eventEmitter: EventEmitter }, | ||
); | ||
|
||
jest.spyOn(Date, 'now').mockImplementation(() => { | ||
throw new Error(); | ||
}); | ||
|
||
await logoutHandler()(req as any, res as any); | ||
|
||
expect(res._getStatusCode()).toBe(500); | ||
expect(res._getData()).toBe( | ||
JSON.stringify({ message: 'An error has occured.' }), | ||
); | ||
|
||
jest.restoreAllMocks(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './passwordCheckHandler'; | ||
export * from './loginHandler'; | ||
export * from './logoutHandler'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Request, Response } from 'express'; | ||
|
||
import { sendJson } from './sendJson'; | ||
import { setCookie } from './setCookie'; | ||
|
||
interface PasswordProtectHandlerOptions { | ||
/* @default next-password-protect */ | ||
cookieName?: string; | ||
cookieSameSite?: boolean | 'lax' | 'none' | 'strict'; | ||
cookieSecure?: boolean; | ||
} | ||
|
||
export const logoutHandler = ( | ||
options?: PasswordProtectHandlerOptions, | ||
) => async (req: Request, res: Response) => { | ||
res.setHeader('Content-Type', 'application/json'); | ||
|
||
try { | ||
if (req.method !== 'POST') { | ||
throw new Error('Invalid method.'); | ||
} | ||
|
||
setCookie(res, options?.cookieName || 'next-password-protect', '', { | ||
httpOnly: true, | ||
sameSite: options?.cookieSameSite || false, | ||
secure: | ||
options?.cookieSecure !== undefined | ||
? options?.cookieSecure | ||
: process.env.NODE_ENV === 'production', | ||
path: '/', | ||
maxAge: 0, | ||
}); | ||
|
||
sendJson(res, 200); | ||
} catch (err) { | ||
sendJson(res, 500, { message: err.message || 'An error has occured.' }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters