Last active
May 31, 2016 15:26
-
-
Save rbezerra/f74b412be180cda7fd2b0e1613ba75f0 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
function login(login, password){ | |
return new Promise(function(resolve, reject){ | |
ad.findUserAsync(login) | |
.then(function(user){ | |
var context = { | |
user: user, | |
password: password | |
}; | |
return resolve(context); | |
}) | |
.catch(function(err){ | |
return reject(err); | |
}); | |
}); | |
} | |
function authenticate(context){ | |
return new Promise(function(resolve, reject){ | |
ad.authenticateAsync(context.user.mail, context.password) | |
.then(function(auth){ | |
context.auth = auth | |
return resolve(context); | |
}) | |
.catch(function(err){ | |
return reject(err); | |
}); | |
}) | |
} | |
login(username, password) | |
.then(authenticate) | |
.then(function(context){ | |
if(!context.auth) return res.status(403).send({"message": "user not authenticated"}); | |
return res.status(200).send({"message": "user authenticated","data": context.user}); | |
}) | |
.catch(function(err){ | |
return next(err); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment