Skip to content

Commit

Permalink
Implement logoutHandler middleware (#525)
Browse files Browse the repository at this point in the history
* Implment `logoutHandler` middleware

* Create changeset
  • Loading branch information
blakewilson committed Oct 1, 2021
1 parent 974d30f commit 4ded997
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fresh-ads-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustjs/core': minor
---

Implement `logoutHandler` middleware
2 changes: 1 addition & 1 deletion packages/core/src/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './server/cookie';
export { authorizeHandler } from './server/middleware';
export { authorizeHandler, logoutHandler } from './server/middleware';
export * from './authorize';
export * from './client/accessToken';
25 changes: 25 additions & 0 deletions packages/core/src/auth/server/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,28 @@ export async function authorizeHandler(
res.end(JSON.stringify({ error: 'Internal Server Error' }));
}
}

/**
* A Node handler for processing incoming requests to logout an authenticated user.
* This handler clears the refresh token from the cookie and returns a response.
*
* @param {IncomingMessage} req
* @param {ServerResponse} res
*
* @see https://faustjs.org/docs/next/guides/auth
*/
export function logoutHandler(req: IncomingMessage, res: ServerResponse): void {
// Only allow POST requests, as browsers may pre-fetch GET requests.
if (req.method !== 'POST') {
res.statusCode = 405;
res.end();

return;
}

const oauth = new OAuth(new Cookies(req, res));
oauth.setRefreshToken(undefined);

res.statusCode = 205;
res.end();
}

0 comments on commit 4ded997

Please sign in to comment.