Refactor accountLogin
@@ -78,41 +78,32 @@ export function accountLogin(credentials, options) {
const { email } = parseCredentials(credentials);
- const prevPrivateId = getIn(state, [
- "router",
- "currentParams",
- "privateId",
- ]);
- const prevEmail = state.getIn(["account", "email"]);
+ const isLoggedIn = getIn(state, ["account", "loggedIn"]);
+ const processingLoginForEmail =
+ getIn(state, ["process", "processingLoginForEmail"]) ||
+ _loginInProcessFor;
- const wasLoggedIn =
- !state.getIn(["process", "processingInitialLoad"]) &&
- (prevPrivateId || prevEmail);
-
- if (
- state.getIn(["process", "processingLoginForEmail"]) === email ||
- _loginInProcessFor === email
- ) {
+ if (processingLoginForEmail) {
console.debug(
"Already logging in as ",
- email,
+ processingLoginForEmail,
". Canceling redundant attempt."
);
return;
}
- if (
- !state.getIn(["process", "processingInitialLoad"]) &&
- ((credentials.privateId && credentials.privateId === prevPrivateId) ||
- (credentials.email && credentials.email === prevEmail))
- ) {
+ if (isLoggedIn && !state.getIn(["process", "processingInitialLoad"])) {
+ const loggedInEmail = state.getIn(["account", "email"]);
+
+ if (credentials.email === loggedInEmail) {
console.debug(
- "Already logged into this account (" +
- (credentials.privateId || credentials.email) +
- "). Aborting second login attempt."
+ "Already loggedIn with (" +
+ credentials.email +
+ "). Aborting login attempt."
);
return;
}
+ }
return Promise.resolve()
.then(() => {
@@ -123,13 +114,16 @@ export function accountLogin(credentials, options) {
});
})
.then(() => {
- if (wasLoggedIn) {
+ if (isLoggedIn) {
return logout().then(() => {
if (
options_.doRedirects &&
- getIn(state, ["router", "currentParams", "privateId"])
+ getIn(state, ["account", "isAnonymous"])
) {
- return stateGoCurrent({ privateId: "" })(dispatch, getState);
+ return stateGoCurrent({ privateId: undefined })(
+ dispatch,
+ getState
+ );
}
});
}
@@ -173,7 +167,7 @@ export function accountLogin(credentials, options) {
error.response.json().then(loginError => {
return Promise.resolve()
.then(() => {
- if (wasLoggedIn) {
+ if (isLoggedIn) {
return dispatch({ type: actionTypes.account.reset });
}
})
@@ -214,9 +208,7 @@ export function accountLogout() {
const state = getState();
if (state.getIn(["process", "processingLogout"]) || _logoutInProcess) {
- console.debug(
- "There's already a logout in process. Aborting redundant attempt."
- );
+ console.debug("Logout in process. Aborting redundant attempt.");
return;
}
_logoutInProcess = true;
