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

 ##########
 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({
 
 Review comment:
   Why call `createAdmin` on a password change?  If this is by design, can we 
consider renaming this function or at least leave a comment explaining why 
we're doing this?
 
----------------------------------------------------------------
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