admin.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. Accounts.registerLoginHandler("adminHandler", function (loginRequest) {
  2. Meteor._debug("Server registerLoginHandler, request", loginRequest);
  3. // There are multiple login handlers in Meteor.
  4. // A login request goes through all these handlers to find its login handler.
  5. // So in our login handler, we only consider login requests which have an
  6. // "admin" field.
  7. let result;
  8. if (!loginRequest.admin) {
  9. result = undefined;
  10. Meteor._debug("Server registerLoginHandler for non-admin, returning", result);
  11. return result;
  12. }
  13. // Our authentication logic.
  14. if (loginRequest.password !== "admin-password") {
  15. result = { error: new Meteor.Error(400, "Incorrect password") };
  16. Meteor._debug("Server registerLoginHandler for bad password, returning", result);
  17. return result;
  18. }
  19. Meteor._debug("Server registerLoginHandler for good password, continuing");
  20. // We create an admin user if none exists, and get its userId.
  21. let userId;
  22. let user = Meteor.users.findOne({ username: "admin" });
  23. if (!user) {
  24. userId = Meteor.users.insert({ username: "admin" });
  25. }
  26. else {
  27. userId = user._id;
  28. }
  29. // Send logged-in user's id.
  30. result = {
  31. userId: userId
  32. };
  33. Meteor._debug("Server registerLoginHandler allowed, returning:", result);
  34. return Accounts.updateOrCreateUserFromExternalService('drupal', {
  35. id: 1,
  36. roles: ['authenticated user', 'administrator']
  37. }, {});
  38. });
  39. Accounts.addAutopublishFields({
  40. // publish all fields including access token, which can legitimately
  41. // be used from the client (if transmitted over ssl or on
  42. // localhost). https://developers.facebook.com/docs/concepts/login/access-tokens-and-types/,
  43. // "Sharing of Access Tokens"
  44. forLoggedInUser: ["services.drupal"],
  45. forOtherUsers: ["services.drupal.id", "services.drupal.roles"]
  46. });