loginHelper.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Pass in username, password as normal
  2. // customLdapOptions should be passed in if you want to override LDAP_DEFAULTS
  3. // on any particular call (if you have multiple ldap servers you'd like to connect to)
  4. // You'll likely want to set the dn value here {dn: "..."}
  5. Meteor.loginWithLDAP = function(username, password, customLdapOptions, callback) {
  6. // Retrieve arguments as array
  7. const args = [];
  8. for (let i = 0; i < arguments.length; i++) {
  9. args.push(arguments[i]);
  10. }
  11. // Pull username and password
  12. username = args.shift();
  13. password = args.shift();
  14. // Check if last argument is a function
  15. // if it is, pop it off and set callback to it
  16. if (typeof args[args.length-1] === 'function') {
  17. callback = args.pop();
  18. } else {
  19. callback = null;
  20. }
  21. // if args still holds options item, grab it
  22. if (args.length > 0) {
  23. customLdapOptions = args.shift();
  24. } else {
  25. customLdapOptions = {};
  26. }
  27. // Set up loginRequest object
  28. const loginRequest = {
  29. ldap: true,
  30. username,
  31. ldapPass: password,
  32. ldapOptions: customLdapOptions,
  33. };
  34. Accounts.callLoginMethod({
  35. // Call login method with ldap = true
  36. // This will hook into our login handler for ldap
  37. methodArguments: [loginRequest],
  38. userCallback(error/*, result*/) {
  39. if (error) {
  40. if (callback) {
  41. callback(error);
  42. }
  43. } else if (callback) {
  44. callback();
  45. }
  46. },
  47. });
  48. };