Fetched User data on Login, modified PrivateRoute logic
This commit is contained in:
@@ -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