Skip to content

Commit 095f114

Browse files
authored
fix: catch invalid user response crash (chaynHQ#1008)
1 parent 7c3a72e commit 095f114

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

components/head/OpenGraphMetadata.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ const OpenGraphMetadata = () => {
1515
<meta property="og:image" content="/preview.png" key="og-image" />
1616
<meta property="og:image:alt" content={imageAltContent} key="og-image-alt" />
1717
<meta name="twitter:card" content="summary_large_image" key="twitter-card" />
18-
<meta
19-
name="twitter:description"
20-
content={twitterDescriptionContent}
21-
key="twitter-desc"
22-
/>
18+
<meta name="twitter:description" content={twitterDescriptionContent} key="twitter-desc" />
2319
{/** PWA specific tags **/}
2420
<link rel="manifest" href="/manifest.json" />
2521
<link rel="apple-touch-icon" href="/icons/apple/icon-120x120.png"></link>

guards/AuthGuard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function AuthGuard({ children }: { children: JSX.Element }) {
4242
const userAuthLoading = useTypedSelector((state) => state.user.authStateLoading);
4343

4444
const { userResourceError } = useLoadUser();
45-
const unauthenticated = userResourceError || (!userAuthLoading && !userLoading && !userId);
45+
const authenticated = !userResourceError && userId && !userAuthLoading && !userLoading;
4646

4747
// Get top level directory of path e.g pathname /courses/course_name has pathHead courses
4848
const pathHead = router.pathname.split('/')[1]; // E.g. courses | therapy | partner-admin
@@ -53,7 +53,7 @@ export function AuthGuard({ children }: { children: JSX.Element }) {
5353
}
5454

5555
// Page requires authenticated user
56-
if (unauthenticated) {
56+
if (!authenticated) {
5757
if (typeof window !== 'undefined') {
5858
router.push(`/auth/login${generateReturnUrlQuery(router.asPath)}`);
5959
}

hooks/useLoadUser.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client';
22

33
import { getAuth, onIdTokenChanged, signOut } from 'firebase/auth';
4-
import { useEffect } from 'react';
4+
import { useEffect, useState } from 'react';
55
import { useCreateEventLogMutation, useGetUserQuery } from '../app/api';
66
import { setAuthStateLoading, setLoadError, setUserLoading, setUserToken } from '../app/userSlice';
77
import { EVENT_LOG_NAME } from '../constants/enums';
@@ -25,7 +25,10 @@ export default function useLoadUser() {
2525
const userAuthLoading = useTypedSelector((state) => state.user.authStateLoading);
2626
const { clearState } = useStateUtils();
2727
const [createEventLog] = useCreateEventLogMutation();
28+
const [isInvalidUserResourceResponse, setIsInvalidUserResourceResponse] =
29+
useState<boolean>(false);
2830

31+
const invalidUserResourceError = 'Invalid user resource success response';
2932
// 1. Listen for firebase auth state or auth token updated, triggered by firebase auth loaded
3033
// When a user token is available, set the token in state to be used in request headers
3134
useEffect(() => {
@@ -60,7 +63,11 @@ export default function useLoadUser() {
6063

6164
// 3a. Handle get user success
6265
useEffect(() => {
63-
if (userResourceIsSuccess && userResource.user.id) {
66+
if (userResourceIsSuccess) {
67+
if (!userResource.user?.id) {
68+
setIsInvalidUserResourceResponse(true);
69+
return;
70+
}
6471
dispatch(setUserLoading(false));
6572

6673
const eventUserData = getEventUserResponseData(userResource);
@@ -71,8 +78,10 @@ export default function useLoadUser() {
7178

7279
// 3b. Handle get user error
7380
useEffect(() => {
74-
if (userResourceError) {
75-
const errorMessage = getErrorMessage(userResourceError) || 'error';
81+
if (userResourceError || isInvalidUserResourceResponse) {
82+
const errorMessage = userResourceError
83+
? getErrorMessage(userResourceError) || 'error'
84+
: invalidUserResourceError;
7685
signOut(auth);
7786
dispatch(setLoadError(errorMessage));
7887
dispatch(setUserLoading(false));
@@ -85,10 +94,14 @@ export default function useLoadUser() {
8594
logEvent(GET_USER_ERROR, { message: errorMessage }); // deprecated event
8695
logEvent(LOGOUT_FORCED);
8796
}
88-
}, [userResourceError, dispatch, auth]);
97+
}, [userResourceError, isInvalidUserResourceResponse, dispatch, auth]);
8998

9099
return {
91100
userResourceIsLoading,
92-
userResourceError,
101+
userResourceError: userResourceError
102+
? getErrorMessage(userResourceError)
103+
: isInvalidUserResourceResponse
104+
? invalidUserResourceError
105+
: undefined,
93106
};
94107
}

0 commit comments

Comments
 (0)