Browse Source

Demo next steps. Still not working.

Frederic G. MARAND 8 years ago
parent
commit
d44f45deec
2 changed files with 46 additions and 0 deletions
  1. 10 0
      client/admin.js
  2. 36 0
      server/admin.js

+ 10 - 0
client/admin.js

@@ -0,0 +1,10 @@
+Meteor.loginAsAdmin = function (password, callback) {
+  // Create a login request with admin:true, so our loginHandler can handle it.
+  let loginRequest = { admin: true, password: password };
+  Meteor._debug('Client loginAsAdmin');
+  // Send the login request.
+  Accounts.callLoginMethod({
+    methodArguments: [loginRequest],
+    userCallback: callback
+  });
+};

+ 36 - 0
server/admin.js

@@ -0,0 +1,36 @@
+Accounts.registerLoginHandler(function (loginRequest) {
+  Meteor._debug("Server registerLoginHandler", loginRequest);
+  // There are multiple login handlers in Meteor.
+  // A login request goes through all these handlers to find its login handler.
+  // So in our login handler, we only consider login requests which have an
+  // "admin" field.
+  let result;
+  if (!loginRequest.admin) {
+    result = void 0;
+    Meteor._debug("Server registerLoginHandler, returning", result);
+    return result;
+  }
+
+  // Our authentication logic.
+  if (loginRequest.password.password !== "admin-password") {
+    result = null;
+    Meteor._debug("Server registerLoginHandler, returning", result);
+    return result;
+  }
+
+  // We create an admin user if none exists, and get its userId.
+  let userId;
+  let user = Meteor.users.findOne({ username: "admin" });
+  if (!user) {
+    userId = Meteor.users.insert({ username: "admin" });
+  }
+  else {
+    userId = user._id;
+  }
+  // Send logged-in user's id.
+  result = {
+    id: userId
+  };
+  Meteor._debug("Server registerLoginHandler, returning", result);
+  return result;
+});