millayr commented on a change in pull request #923: Clean up Auth section
URL: https://github.com/apache/couchdb-fauxton/pull/923#discussion_r119733564
 
 

 ##########
 File path: app/addons/auth/actions.js
 ##########
 @@ -10,148 +10,157 @@
 // License for the specific language governing permissions and limitations 
under
 // the License.
 import FauxtonAPI from "../../core/api";
-import ActionTypes from "./actiontypes";
+import app from "../../app";
 import ClusterStore from "../cluster/cluster.stores";
+import ActionTypes from './actiontypes';
+import Api from './api';
 
-var nodesStore = ClusterStore.nodesStore;
+const {
+  AUTH_PASSWORD_UPDATED,
+  AUTH_SHOW_PASSWORD_MODAL,
+  AUTH_HIDE_PASSWORD_MODAL,
+  AUTH_ADMIN_CREATED
+} = ActionTypes;
 
-var errorHandler = function (xhr, type, msg) {
-  msg = xhr;
-  if (arguments.length === 3) {
-    msg = xhr.responseJSON.reason;
-  }
+const nodesStore = ClusterStore.nodesStore;
 
+function errorHandler({ message }) {
   FauxtonAPI.addNotification({
-    msg: msg,
-    type: 'error'
+    msg: message,
+    type: "error"
   });
 };
 
+function validate(...predicates) {
+  return predicates.every(isTrue => isTrue);
+}
+
+export const validateUser = (username, password) => {
+  return validate(!_.isEmpty(username), !_.isEmpty(password));
+};
 
-function login (username, password, urlBack) {
-  var promise = FauxtonAPI.session.login(username, password);
+export const validatePasswords = (password, passwordConfirm) => {
+  return validate(
+    !_.isEmpty(password),
+    !_.isEmpty(passwordConfirm),
+    password === passwordConfirm
+  );
+};
 
-  promise.then(() => {
-    FauxtonAPI.addNotification({ msg: FauxtonAPI.session.messages.loggedIn });
-    if (urlBack) {
-      return FauxtonAPI.navigate(urlBack);
+export const login = (username, password, urlBack) => {
+  if (!validateUser(username, password)) {
+    return errorHandler({message: app.i18n.en_US['auth-missing-credentials']});
+  }
+
+  return Api.login({name: username, password})
+  .then(resp => {
+    if (resp.error) {
+      return errorHandler({message: resp.reason});
     }
-    FauxtonAPI.navigate('/');
-  }, errorHandler);
-}
 
-function changePassword (password, passwordConfirm) {
-  var nodes = nodesStore.getNodes();
-  var promise = FauxtonAPI.session.changePassword(password, passwordConfirm, 
nodes[0].node);
+    let msg = app.i18n.en_US['auth-logged-in'];
+    if (msg) {
+      FauxtonAPI.addNotification({msg});
+    }
 
-  promise.then(() => {
-    FauxtonAPI.addNotification({ msg: 
FauxtonAPI.session.messages.changePassword });
-    FauxtonAPI.dispatch({ type: ActionTypes.AUTH_CLEAR_CHANGE_PWD_FIELDS });
-  }, errorHandler);
-}
+    if (urlBack && !urlBack.includes("login")) {
+      return FauxtonAPI.navigate(urlBack);
+    }
+    FauxtonAPI.navigate("/");
+  })
+  .catch(errorHandler);
+};
 
-function updateChangePasswordField (value) {
-  FauxtonAPI.dispatch({
-    type: ActionTypes.AUTH_UPDATE_CHANGE_PWD_FIELD,
-    value: value
+export const passwordUpdated = () => {
+  FauxtonAPI.addNotification({
+    msg: app.i18n.en_US["auth-change-password"]
   });
-}
 
-function updateChangePasswordConfirmField (value) {
-  FauxtonAPI.dispatch({
-    type: ActionTypes.AUTH_UPDATE_CHANGE_PWD_CONFIRM_FIELD,
-    value: value
-  });
-}
+  return {
+    type: AUTH_PASSWORD_UPDATED
+  };
+};
 
-function createAdmin (username, password, loginAfter) {
+export const changePassword = (username, password, passwordConfirm) => 
dispatch => {
+  if (!validatePasswords(password, passwordConfirm)) {
+    return errorHandler({message: 
app.i18n.en_US['auth-passwords-not-matching']});
+  }
   var nodes = nodesStore.getNodes();
-  var promise = FauxtonAPI.session.createAdmin(username, password, loginAfter, 
nodes[0].node);
+  Api.createAdmin({
+    name: username,
+    password,
+    node: nodes[0].node
+  }).then(
+    () => {
+      dispatch(passwordUpdated());
+    },
+    errorHandler
+  );
+};
 
-  promise.then(() => {
-    FauxtonAPI.addNotification({ msg: FauxtonAPI.session.messages.adminCreated 
});
-    if (loginAfter) {
-      FauxtonAPI.navigate('/');
-    } else {
-      FauxtonAPI.dispatch({ type: ActionTypes.AUTH_CLEAR_CREATE_ADMIN_FIELDS 
});
-    }
-  }, (xhr, type, msg) => {
-    msg = xhr;
-    if (arguments.length === 3) {
-      msg = xhr.responseJSON.reason;
+export const adminCreated = () => {
+  return {
+    type: AUTH_ADMIN_CREATED
+  };
+};
+
+export const createAdmin = (username, password, loginAfter) => (dispatch) => {
+  const node = nodesStore.getNodes()[0].node;
+  if (!validateUser(username, password)) {
+    return errorHandler({message: app.i18n.en_US['auth-missing-credentials']});
+  }
+
+  Api.createAdmin({name: username, password, node})
+  .then(resp => {
+    if (resp.error) {
+      return errorHandler({message: 
`${app.i18n.en_US['auth-admin-creation-failed-prefix']} ${resp.reason}`});
     }
-    errorHandler(FauxtonAPI.session.messages.adminCreationFailedPrefix + ' ' + 
msg);
-  });
-}
 
-// simple authentication method - does nothing other than check creds
-function authenticate (username, password, onSuccess) {
-  $.ajax({
-    cache: false,
-    type: 'POST',
-    url: '/_session',
-    dataType: 'json',
-    data: { name: username, password: password }
-  }).then(() => {
-    FauxtonAPI.dispatch({
-      type: ActionTypes.AUTH_CREDS_VALID,
-      options: { username: username, password: password }
-    });
-    hidePasswordModal();
-    onSuccess(username, password);
-  }, () => {
     FauxtonAPI.addNotification({
-      msg: 'Your password is incorrect.',
-      type: 'error',
-      clear: true
+      msg: app.i18n.en_US['auth-admin-created']
     });
-    FauxtonAPI.dispatch({
-      type: ActionTypes.AUTH_CREDS_INVALID,
-      options: { username: username, password: password }
-    });
-  });
-}
-
-function updateCreateAdminUsername (value) {
-  FauxtonAPI.dispatch({
-    type: ActionTypes.AUTH_UPDATE_CREATE_ADMIN_USERNAME_FIELD,
-    value: value
-  });
-}
 
-function updateCreateAdminPassword (value) {
-  FauxtonAPI.dispatch({
-    type: ActionTypes.AUTH_UPDATE_CREATE_ADMIN_PWD_FIELD,
-    value: value
+    dispatch(adminCreated());
+    if (loginAfter) {
+      return FauxtonAPI.navigate("/login");
+    }
   });
-}
+};
 
-function selectPage (page) {
-  FauxtonAPI.dispatch({
-    type: ActionTypes.AUTH_SELECT_PAGE,
-    page: page
-  });
+// simple authentication method - does nothing other than check creds
+export function authenticate(username, password, onSuccess) {
 
 Review comment:
   Consider being consistent with arrow functions here.  I'm perhaps overly 
picky when it comes to consistency :)
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to