Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 093c7bb

Browse files
committed
Update isLoggedIn for all routes
1 parent 936b01a commit 093c7bb

13 files changed

+64
-54
lines changed

src/app/apiFetch/Leaderboard.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* tslint:disable:no-console*/
2-
import { jsonResponseWrapper } from 'app/apiFetch/utils';
2+
import { jsonResponseWrapper, textResponseWrapper } from 'app/apiFetch/utils';
33
import * as LeaderboardInterfaces from 'app/types/Leaderboard';
44
import { API_BASE_URL } from '../../config/config';
55

@@ -120,7 +120,7 @@ export const getTimer = () => {
120120
},
121121
method: 'GET',
122122
})
123-
.then((response) => response.text())
123+
.then((response) => textResponseWrapper(response))
124124
.then((data) => data)
125125
.catch((err) => {
126126
console.error(err);

src/app/apiFetch/MatchView.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ export const getMatches = (pageNo: number, pageSize: number) => {
99
method: 'GET',
1010
})
1111
.then((response) => {
12-
console.log('match response');
13-
console.log(response);
14-
15-
return response.json();
12+
return jsonResponseWrapper(response);
1613
})
1714
.then((data) => {
1815
return data;
@@ -32,10 +29,7 @@ export const getTopMatches = (pageNo: number, pageSize: number) => {
3229
method: 'GET',
3330
})
3431
.then((response) => {
35-
console.log('top response');
36-
console.log(response);
37-
38-
return response.json();
32+
return jsonResponseWrapper(response);
3933
})
4034
.then((data) => {
4135
return data;
@@ -52,7 +46,6 @@ export const getGameLogs = (gameId: number) => {
5246
method: 'GET',
5347
})
5448
.then((response) => {
55-
console.log(response);
5649
return jsonResponseWrapper(response);
5750
})
5851
.then((data) => data)

src/app/apiFetch/Notification.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ export const getUnreadNotifications = () => {
1717
},
1818
method: 'GET',
1919
})
20-
.then((response) => response.json())
20+
.then((response) => jsonResponseWrapper(response))
2121
.then((data) => data)
2222
.catch((error) => {
23-
console.log(error);
23+
console.error(error);
2424
});
2525
};
2626

src/app/apiFetch/utils/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ export enum HeadReqType {
1515
*/
1616
export function jsonResponseWrapper(response: any) {
1717
return response.json().then((data: any) => {
18-
// tslint:disable-next-line: no-console
19-
console.log(data);
2018
return new Promise((resolve, reject) => {
2119
let type: string = resType.SUCCESS;
2220
let error: string = '';
@@ -29,6 +27,7 @@ export function jsonResponseWrapper(response: any) {
2927
error,
3028
type,
3129
body: data,
30+
status: response.status,
3231
});
3332
});
3433
});
@@ -74,6 +73,7 @@ export function headResponseWrapper(response: any, headReqType: HeadReqType) {
7473
error,
7574
type,
7675
body: response.text(),
76+
status: response.status,
7777
});
7878
});
7979
}
@@ -101,6 +101,7 @@ export function textResponseWrapper(response: any) {
101101
error,
102102
type,
103103
body: data,
104+
status: response.status,
104105
});
105106
});
106107
});

src/app/components/MatchView/MatchElement.tsx

-5
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ export class MatchElement extends React.Component<
3333

3434
let games = null;
3535

36-
// tslint:disable-next-line: no-console
37-
console.log(match);
38-
3936
if (isSelected) {
4037
games = match.games.map((game, index) => {
4138
let gameVerdict = 'TIE';
@@ -55,8 +52,6 @@ export class MatchElement extends React.Component<
5552
key={index}
5653
className={classnames(styles.game, 'mt-2')}
5754
onClick={(e) => {
58-
// tslint:disable-next-line: no-console
59-
console.log('HE CLICKED HERE');
6055
getGameLogs(game.id);
6156
e.stopPropagation();
6257
}}

src/app/components/Renderer.tsx

-11
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ export default class Renderer extends React.Component<RendererInterfaces.Props,
2222
clearLog,
2323
updateLog,
2424
} = this.props;
25-
// tslint:disable-next-line: no-console
26-
console.log(
27-
'logfile:',
28-
logFile,
29-
'player1DL',
30-
player1DebugLog,
31-
'player2DL',
32-
player2DebugLog,
33-
'MatchPlayerId',
34-
matchPlayerId,
35-
);
3625

3726
return (
3827
<div style={{ height, display: 'flex', width: '100%', alignItems: 'center' }}>

src/app/components/SocketHandler.tsx

-8
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ export class SocketHandler extends React.Component<SocketHandlerInterfaces.Props
6565
clearAllLogs,
6666
} = this.props;
6767
const matchPlayerId = parseInt(matchDetails.matchPlayerId, 10);
68-
// tslint:disable-next-line: no-console
69-
console.log(matchDetails, matchPlayerId, userId);
7068

7169
clearDisplayDebugLog();
7270
clearAllLogs();
@@ -128,8 +126,6 @@ export class SocketHandler extends React.Component<SocketHandlerInterfaces.Props
128126
} = this.props;
129127
switch (request) {
130128
case SubmissionInterfaces.Request.PREVIOUS_COMMIT_MATCH: {
131-
// tslint:disable-next-line: no-console
132-
console.log('INITIATING MATCH:PREVIOUS_COMMIT_MATCH');
133129
this.initiateMatch(
134130
userId,
135131
userId,
@@ -141,8 +137,6 @@ export class SocketHandler extends React.Component<SocketHandlerInterfaces.Props
141137
break;
142138
}
143139
case SubmissionInterfaces.Request.AI_MATCH: {
144-
// tslint:disable-next-line: no-console
145-
console.log('INITIATING MATCH:AI_MATCH');
146140
this.initiateMatch(
147141
playerId1,
148142
playerId2,
@@ -154,8 +148,6 @@ export class SocketHandler extends React.Component<SocketHandlerInterfaces.Props
154148
break;
155149
}
156150
case SubmissionInterfaces.Request.SELF_MATCH: {
157-
// tslint:disable-next-line: no-console
158-
console.log('INITIATING MATCH:SELF_MATCH');
159151
this.initiateMatch(
160152
userId,
161153
playerId2,

src/app/sagas/Leaderboard.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* tslint:disable:no-console*/
22
import { LeaderboardActions, SubmissionActions } from 'app/actions';
33
import * as LeaderboardFetch from 'app/apiFetch/Leaderboard';
4+
import { checkAuthentication } from 'app/sagas/utils';
45
import { Request } from 'app/types/code/Submission';
56
import { resType } from 'app/types/sagas';
67
import { all, call, put, takeEvery } from 'redux-saga/effects';
@@ -13,6 +14,8 @@ export function* getLeaderboard(action: ActionType<typeof LeaderboardActions.get
1314
pageNo: action.payload.pageNo,
1415
pageSize: action.payload.pageSize,
1516
});
17+
const isAuthenticated = yield checkAuthentication(result);
18+
if (isAuthenticated === false) return;
1619
yield put(LeaderboardActions.updateError(result.error));
1720
if (result.type !== resType.ERROR) {
1821
yield put(
@@ -36,6 +39,8 @@ export function* getLeaderboardByDiv(
3639
pageNo: action.payload.pageNo,
3740
pageSize: action.payload.pageSize,
3841
});
42+
const isAuthenticated = yield checkAuthentication(result);
43+
if (isAuthenticated === false) return;
3944
yield put(LeaderboardActions.updateError(result.error));
4045
if (result.type !== resType.ERROR) {
4146
yield put(
@@ -59,6 +64,8 @@ export function* getLeaderboardByUserType(
5964
pageNo: action.payload.pageNo,
6065
pageSize: action.payload.pageSize,
6166
});
67+
const isAuthenticated = yield checkAuthentication(result);
68+
if (isAuthenticated === false) return;
6269
yield put(LeaderboardActions.updateError(result.error));
6370
if (result.type !== resType.ERROR) {
6471
yield put(
@@ -83,6 +90,8 @@ export function* getLeaderboardLeaderboardByDivAndType(
8390
pageNo: action.payload.pageNo,
8491
pageSize: action.payload.pageSize,
8592
});
93+
const isAuthenticated = yield checkAuthentication(result);
94+
if (isAuthenticated === false) return;
8695
yield put(LeaderboardActions.updateError(result.error));
8796
if (result.type !== resType.ERROR) {
8897
yield put(
@@ -107,6 +116,8 @@ export function* getLeaderboardSearch(
107116
pageSize: action.payload.pageSize,
108117
username: action.payload.username,
109118
});
119+
const isAuthenticated = yield checkAuthentication(result);
120+
if (isAuthenticated === false) return;
110121
yield put(LeaderboardActions.updateError(result.okay));
111122

112123
if (result.type !== resType.ERROR) {
@@ -124,7 +135,9 @@ export function* getLeaderboardSearch(
124135
export function* getTimerSaga() {
125136
try {
126137
const result = yield call(LeaderboardFetch.getTimer);
127-
const time = Number(result);
138+
const isAuthenticated = yield checkAuthentication(result);
139+
if (isAuthenticated === false) return;
140+
const time = Number(result.body);
128141
yield put(LeaderboardActions.setTimer(time));
129142
} catch (err) {
130143
console.error(err);

src/app/sagas/MatchView.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export function* getMatches(action: ActionType<typeof MatchActions.getMatches>)
1616
const isAuthenticated = yield checkAuthentication(res);
1717
if (isAuthenticated === false) return;
1818

19-
if (isArray(res)) {
19+
if (isArray(res.body)) {
2020
const username = yield select((state: RootState) => state.user.username);
21-
const matchData = mapMatchResponse(res, username);
21+
const matchData = mapMatchResponse(res.body, username);
2222
yield put(MatchActions.updateMatches(matchData));
2323
} else yield put(NotificationActions.error(res.message));
2424
} catch (err) {
@@ -39,9 +39,9 @@ export function* getTopMatches(action: ActionType<typeof MatchActions.getTopMatc
3939
const isAuthenticated = yield checkAuthentication(res);
4040
if (isAuthenticated === false) return;
4141

42-
if (isArray(res)) {
42+
if (isArray(res.body)) {
4343
const username = yield select((state: RootState) => state.user.username);
44-
const matchData = mapMatchResponse(res, username);
44+
const matchData = mapMatchResponse(res.body, username);
4545
yield put(MatchActions.updateTopMatches(matchData));
4646
} else yield put(NotificationActions.error(res.message));
4747
} catch (err) {
@@ -52,13 +52,14 @@ export function* getTopMatches(action: ActionType<typeof MatchActions.getTopMatc
5252
export function* getGameLogs(action: ActionType<typeof MatchActions.getGameLogs>) {
5353
try {
5454
const res = yield call(MatchFetch.getGameLogs, action.payload.gameId);
55+
const isAuthenticated = yield checkAuthentication(res);
56+
if (isAuthenticated === false) return;
5557

5658
if (res.type === resType.ERROR) {
5759
yield put(MatchActions.updateError(res.error));
5860
} else {
5961
yield put(GameLogActions.setHideDebugLog(true));
6062
yield put(GameLogActions.clearDisplayDebugLog());
61-
console.log('GameLOG from backend', res);
6263

6364
const userId = yield select((state: RootState) => state.user.userId);
6465

src/app/sagas/Notification.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export function* getUnreadGlobalNotifications(
1818
return;
1919
}
2020

21-
const notifications = res.notifications;
21+
const notifications = res.body.notifications;
22+
23+
if (notifications && notifications.length > 0) {
24+
yield put(NotificationActions.info('You have some unread notifications'));
25+
}
2226

2327
notifications.forEach((notification: Notification) => {
2428
NotificationActions.info(notification.message);
@@ -37,6 +41,11 @@ export function* getAllGlobalNotifications(
3741
) {
3842
try {
3943
const res = yield call(NotificationFetch.getAllGlobalNotifications);
44+
const isAuthenticated = yield checkAuthentication(res);
45+
46+
if (isAuthenticated === false) {
47+
return;
48+
}
4049
const notifications = res.body;
4150
yield put(NotificationActions.updateGlobalNotifications(notifications));
4251
} catch (err) {
@@ -49,8 +58,17 @@ export function* getAllGlobalAnnouncements(
4958
) {
5059
try {
5160
const res = yield call(NotificationFetch.getAllGlobalAnnouncements);
61+
62+
const isAuthenticated = yield checkAuthentication(res);
63+
64+
if (isAuthenticated === false) {
65+
return;
66+
}
5267
const announcements = res.body;
5368
yield put(NotificationActions.updateGlobalAnnouncements(announcements));
69+
if (announcements && announcements.length > 0) {
70+
yield put(NotificationActions.info('You have some unread announcements'));
71+
}
5472
} catch (err) {
5573
console.error(err);
5674
}

src/app/sagas/Submission.ts

-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ export function* previousCommitMatch(
4040

4141
yield put(CodeActions.save());
4242

43-
console.log(action.payload);
44-
4543
yield put(
4644
SubmissionActions.changeStateCurrentRequest(
4745
RequestState.COMPILE_PREVIOUS_COMMIT_CODE,

src/app/sagas/User.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ export function* logout(action: ActionType<typeof UserActions.logout>) {
8484
try {
8585
const res = yield call(UserFetch.userLogout);
8686

87-
const isAuthenticated = yield checkAuthentication({
88-
error: res.error,
89-
type: res.type,
90-
});
87+
const isAuthenticated = yield checkAuthentication(res);
9188
if (isAuthenticated === false) return;
9289

9390
yield put(UserActions.updateErrorMessage(res.message));
@@ -111,6 +108,12 @@ export function* register(action: ActionType<typeof UserActions.register>) {
111108
try {
112109
const res = yield call(UserFetch.userRegister, action.payload.registerDetails);
113110

111+
const isAuthenticated = yield checkAuthentication(res);
112+
113+
if (isAuthenticated === false) {
114+
return;
115+
}
116+
114117
// res.error has error string if type = 'Error', else empty
115118
yield put(UserActions.updateErrorMessage(res.error ? res.body.message : ''));
116119

@@ -133,6 +136,7 @@ export function* register(action: ActionType<typeof UserActions.register>) {
133136
export function* getUserDetails(action: ActionType<typeof UserActions.getUserDetails>) {
134137
try {
135138
const res = yield call(UserFetch.userGetDetails);
139+
136140
// res.error has error string if type = 'Error', else empty
137141
yield put(UserActions.updateErrorMessage(res.error));
138142
const isAuthenticated = yield checkAuthentication(res);
@@ -223,6 +227,12 @@ export function* checkUsernameExists(action: ActionType<typeof UserActions.check
223227
try {
224228
const res = yield call(UserFetch.checkUsernameExists, action.payload.username);
225229

230+
const isAuthenticated = yield checkAuthentication(res);
231+
232+
if (isAuthenticated === false) {
233+
return;
234+
}
235+
226236
// Call returns error if email already exists, else empty
227237
yield put(UserActions.updateErrorMessage(res.error));
228238
} catch (err) {

src/app/sagas/utils/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import * as MatchVewInterfaces from 'app/types/MatchView';
44
import { put } from 'redux-saga/effects';
55

66
export interface ResponseStructure {
7+
status: number | undefined;
78
type: string | undefined;
89
error: string | undefined;
910
}
1011

1112
export function* checkAuthentication(result: ResponseStructure) {
12-
if (result && result.error && result.error === 'Unauthorised') {
13+
if (result && (result.status === 401 || result.status === 403)) {
1314
yield put(
1415
UserActions.updateUserDetails({
1516
country: '',
@@ -18,7 +19,6 @@ export function* checkAuthentication(result: ResponseStructure) {
1819
username: '',
1920
}),
2021
);
21-
yield put(UserActions.setIsAuthenticationOpen(true));
2222
return false;
2323
}
2424
return true;

0 commit comments

Comments
 (0)