Skip to content

Commit 2734650

Browse files
authored
fix: issue 133 (#203)
* <fix> issue_133 * <fix> issue_133 - code updated
1 parent ccc4f13 commit 2734650

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

src/actions/auth.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
*/
55

66
import { createActions } from "redux-actions";
7-
import { decodeToken } from "../utils/token";
7+
import { decodeToken, readCookie } from "../utils/token";
88
import { getApiV3, getApiV5 } from "../services/challenge-api";
99
import { setErrorIcon, ERROR_ICON_TYPES } from "../utils/errors";
1010
import { getAuthUserTokens } from "@topcoder/micro-frontends-navbar-app";
11+
import { TOKEN_COOKIE_KEYS } from "../constants/index";
1112

1213
/**
1314
* Helper method that checks for HTTP error response v5 and throws Error in this case.
@@ -83,11 +84,27 @@ async function setAuthDone() {
8384
return user;
8485
}
8586

87+
/**
88+
* @static
89+
* @desc Check token cookies to find if a user is logged out:
90+
* This is because all the token cookies are cleared if a user is logged out.
91+
* @return {Action}
92+
*/
93+
function checkIsLoggedOut() {
94+
const tokenKeys = Object.keys(TOKEN_COOKIE_KEYS);
95+
const isLoggedOut = _.every(
96+
tokenKeys,
97+
(k) => readCookie(TOKEN_COOKIE_KEYS[k]) === undefined
98+
);
99+
return { isLoggedOut };
100+
}
101+
86102
export default createActions({
87103
AUTH: {
88104
LOAD_PROFILE: loadProfileDone,
89105
SET_TC_TOKEN_V2: setTcTokenV2,
90106
SET_TC_TOKEN_V3: setTcTokenV3,
91107
SET_AUTH_DONE: setAuthDone,
108+
CHECK_IS_LOGGED_OUT: checkIsLoggedOut,
92109
},
93110
});

src/constants/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,9 @@ export const COMPETITION_TRACKS = {
9696
DEV: "Development",
9797
QA: "Quality Assurance",
9898
};
99+
100+
export const TOKEN_COOKIE_KEYS = {
101+
V3JWT: "v3jwt",
102+
TCJWT: "tcjwt",
103+
TCSSO: "tcsso",
104+
};

src/containers/Submission/index.jsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { navigate } from "@reach/router";
55
import { PrimaryButton } from "components/Buttons";
66
import AccessDenied from "components/AccessDenied";
77
import LoadingIndicator from "components/LoadingIndicator";
8+
import { login } from "@topcoder/micro-frontends-navbar-app";
89
import { ACCESS_DENIED_REASON, CHALLENGES_URL } from "../../constants";
910
import Submit from "./Submit";
1011
import actions from "../../actions";
@@ -25,6 +26,7 @@ const Submission = ({
2526
getCommunityList,
2627
isLoadingChallenge,
2728
isChallengeLoaded,
29+
2830
track,
2931
agreed,
3032
filePickers,
@@ -47,6 +49,7 @@ const Submission = ({
4749
setFilePickerUploadProgress,
4850
setFilePickerDragged,
4951
setSubmissionFilestackData,
52+
checkIsLoggedOut,
5053
setAuth,
5154
}) => {
5255
const propsRef = useRef();
@@ -93,8 +96,14 @@ const Submission = ({
9396
}
9497

9598
const handleSubmit = async (data) => {
96-
const registered = await getIsRegistered(challengeId, userId);
97-
if (registered) submit(data);
99+
const isLoggedOut = checkIsLoggedOut();
100+
if (isLoggedOut) {
101+
window.sessionStorage && window.sessionStorage.clear();
102+
login();
103+
} else {
104+
const registered = await getIsRegistered(challengeId, userId);
105+
if (registered) submit(data);
106+
}
98107
};
99108

100109
return (
@@ -170,6 +179,7 @@ Submission.propTypes = {
170179
setFilePickerDragged: PT.func,
171180
setSubmissionFilestackData: PT.func,
172181
setAuth: PT.func,
182+
checkIsLoggedOut: PT.func,
173183
};
174184

175185
const mapStateToProps = (state, ownProps) => {
@@ -215,8 +225,14 @@ const mapDispatchToProps = (dispatch) => {
215225
setAuth: () => {
216226
dispatch(actions.auth.setAuthDone());
217227
},
228+
checkIsLoggedOut: () => {
229+
const action = dispatch(actions.auth.checkIsLoggedOut());
230+
return action?.payload?.isLoggedOut;
231+
},
218232
getIsRegistered: async (challengeId, userId) => {
219-
const action = await dispatch(actions.challenge.getIsRegistered(challengeId, userId));
233+
const action = await dispatch(
234+
actions.challenge.getIsRegistered(challengeId, userId)
235+
);
220236
return action?.payload?.isRegistered;
221237
},
222238
getChallenge: (challengeId) => {

src/utils/token.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ function parseCookie(cookie) {
8787
);
8888
}
8989

90-
function readCookie(name) {
90+
export function readCookie(name) {
9191
return parseCookie(document.cookie)[name];
9292
}

0 commit comments

Comments
 (0)