Fetched User data on Login, modified PrivateRoute logic
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
43
src/sagas/user.sagas.js
Normal 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({}));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user