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

View File

@@ -11,6 +11,7 @@ import {
setFormPasswordConfirmation,
setFormOldPassword
} from "../actions/auth/reducer.actions";
import { setSelfUser } from "../actions/user/reducer.actions";
import {
registerUser,
verifyEmail,
@@ -156,6 +157,7 @@ export function* logoutUserFlow(request) {
yield effects.put(clearAuthRequestError());
yield effects.call(logoutUserCall);
yield effects.put(setSelfUserToken(""));
yield effects.put(setSelfUser({}));
}
export function* changePasswordFlow(request) {

View File

@@ -17,6 +17,12 @@ import {
forgotPasswordFlow,
resetPasswordFlow,
} from "./auth.sagas";
import {
SEND_GET_SELF_USER_REQUEST
} from "../constants/user.constants";
import {
getSelfUserFlow
} from "./user.sagas";
export default function* rootSaga() {
yield takeLatest(SEND_REGISTER_REQUEST, registerUserFlow);
@@ -26,4 +32,5 @@ export default function* rootSaga() {
yield takeLatest(SEND_CHANGE_PASSWORD_REQUEST, changePasswordFlow);
yield takeLatest(SEND_FORGOT_PASSWORD_REQUEST, forgotPasswordFlow);
yield takeLatest(SEND_RESET_PASSWORD_REQUEST, resetPasswordFlow);
yield takeLatest(SEND_GET_SELF_USER_REQUEST, getSelfUserFlow);
}

43
src/sagas/user.sagas.js Normal file
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({}));
}
}