Fetched User data on Login, modified PrivateRoute logic

This commit is contained in:
Alexander Wong
2017-09-03 16:24:43 -06:00
parent 4cadf5df3a
commit e69773ac8e
15 changed files with 295 additions and 28 deletions
+43
View File
@@ -0,0 +1,43 @@
import { effects } from "redux-saga";
import { setSelfUserToken } from "../actions/auth/reducer.actions";
import {
isSendingUserRequest,
setUserRequestError,
setUserRequestSuccess,
clearUserRequestError,
clearUserRequestSuccess,
setSelfUser
} from "../actions/user/reducer.actions";
import { getSelfUser } from "../api/user.api";
function* getSelfUserCall() {
yield effects.put(isSendingUserRequest(true));
try {
const wasSuccessful = yield effects.call(getSelfUser);
yield effects.put(setUserRequestSuccess(wasSuccessful));
yield effects.put(clearUserRequestError());
// Check if the user exists, if yes set the user, otherwise force logout
if (wasSuccessful.results && wasSuccessful.results.length) {
yield effects.put(setSelfUser(wasSuccessful.results[0]));
} else {
yield effects.put(setSelfUserToken(""));
yield effects.put(setSelfUser({}));
}
return wasSuccessful;
} catch (exception) {
yield effects.put(setUserRequestError(exception));
return false;
} finally {
yield effects.put(isSendingUserRequest(false));
}
}
export function* getSelfUserFlow(request) {
yield effects.put(clearUserRequestSuccess());
yield effects.put(clearUserRequestError());
const wasSuccessful = yield effects.call(getSelfUserCall);
if (!wasSuccessful) {
yield effects.put(setSelfUserToken(""));
yield effects.put(setSelfUser({}));
}
}