In my code I have to login a user with username and password. But if the user sends his e-mail instead of his username, I have to resolve it with another request.
This is what the code looks like:
exports.auth = (request, response, next) ->
username = request.body.username
if request.body.email
users.findByEmail request.body.email, (error, result) ->
return next new InternalServerError(error) if error
username = result[0].username
auth.auth username, request.body.password, (error, result) ->
return next new InternalServerError(error) if error
response.json responseData
else
auth.auth username, request.body.password, (error, result) ->
return next new InternalServerError(error) if error
response.json responseData
As you can see there are 3 duplicate lines.
Does anyone know a good code style do make this better? Maybe with Promises? Async Waterfall? Waitfor?