diff --git a/striker-ui-api/src/lib/request_handlers/auth/index.ts b/striker-ui-api/src/lib/request_handlers/auth/index.ts index 6cc1e6e2..8c008ef4 100644 --- a/striker-ui-api/src/lib/request_handlers/auth/index.ts +++ b/striker-ui-api/src/lib/request_handlers/auth/index.ts @@ -1 +1,2 @@ export * from './login'; +export * from './logout'; diff --git a/striker-ui-api/src/lib/request_handlers/auth/logout.ts b/striker-ui-api/src/lib/request_handlers/auth/logout.ts new file mode 100644 index 00000000..c6b28e24 --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/auth/logout.ts @@ -0,0 +1,17 @@ +import { RequestHandler } from 'express'; + +import { stdout } from '../../shell'; + +export const logout: RequestHandler = (request, response) => { + request.session.destroy((error) => { + let scode = 204; + + if (error) { + scode = 500; + + stdout(`Failed to destroy session upon logout; CAUSE: ${error}`); + } + + response.status(scode).send(); + }); +}; diff --git a/striker-ui-api/src/routes/auth.ts b/striker-ui-api/src/routes/auth.ts index 0a2475e1..2be436ef 100644 --- a/striker-ui-api/src/routes/auth.ts +++ b/striker-ui-api/src/routes/auth.ts @@ -1,10 +1,13 @@ import express from 'express'; -import { login } from '../lib/request_handlers/auth'; +import { assertAuthentication } from '../lib/assertAuthentication'; +import { login, logout } from '../lib/request_handlers/auth'; import passport from '../passport'; const router = express.Router(); -router.post('/login', passport.authenticate('login'), login); +router + .post('/login', passport.authenticate('login'), login) + .put('/logout', assertAuthentication(), logout); export default router;