Skip to content

Instantly share code, notes, and snippets.

@whtswrng
Last active November 8, 2018 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whtswrng/57fa348b3fb8e129d6c10998e094ba4b to your computer and use it in GitHub Desktop.
Save whtswrng/57fa348b3fb8e129d6c10998e094ba4b to your computer and use it in GitHub Desktop.
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