Skip to content

Instantly share code, notes, and snippets.

@rbezerra
Last active May 31, 2016 15:26
Show Gist options
  • Save rbezerra/f74b412be180cda7fd2b0e1613ba75f0 to your computer and use it in GitHub Desktop.
Save rbezerra/f74b412be180cda7fd2b0e1613ba75f0 to your computer and use it in GitHub Desktop.
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