A few days during the implementation of a middleware that allows the authentication of users through the LDAP protocol, I found myself faced with a major problem: the module “ldapauth-fork” that I used is not compatible with the Promise, and this does not allow the proper use of data.
Below I leave you the solution that I found by doing various tests with the module “bluebird”.
As a first step we will have to create a “router” in which we will define the configuration of our API.
Now we create our middleware that will call the LDAP services and authenticate and export it as a Node module.
Now we’ll just need a controller inside which we’ll take the data passed into the POST call and pass it to our Promise. As you can see we call the method “authenticate” inside the Promise in order to steal the data related to the user’s profile and save it in a constant “profile”.
From this point on with the content of the constant “profile” we can do what we want: generate a BEARER token or read its content, parse it and call us more methods above and then return it all to the response of our API.