-
-
Save whtswrng/57fa348b3fb8e129d6c10998e094ba4b to your computer and use it in GitHub Desktop.
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
const {INTERNAL_SERVER_ERROR, OK } = require('http-status-codes'); | |
async function login(request, response, loginAction) { | |
try { | |
const actionResult = await loginAction(request.body); | |
handleResponse(request, response, actionResult); | |
} catch (e) { | |
sendInternalServerError(response); | |
} | |
} | |
function handleResponse(request, response, actionResult) { | |
if (result(actionResult).hasStatusCode(OK)) { | |
sendSuccessfulResponse(response, request, actionResult); | |
} else { | |
sendInternalServerError(response); | |
} | |
} | |
function sendSuccessfulResponse(response, request, actionResult) { | |
const headers = {'set-cookie': actionResult.headers['set-cookie']}; | |
const body = {}; | |
response.send(OK, body, headers); | |
} | |
function sendInternalServerError(response) { | |
response.send(INTERNAL_SERVER_ERROR, { errorMessage: 'An unexpected error occurred.' }); | |
} | |
function result(actionResult) { | |
return { | |
hasStatusCode: statusCode => { | |
return actionResult.statusCode === statusCode; | |
}, | |
}; | |
} | |
module.exports = login; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment