From ac48e9c741004118df60ad39a585976fc7a68281 Mon Sep 17 00:00:00 2001 From: Will Zhang Date: Thu, 10 Oct 2024 13:35:06 -0400 Subject: [PATCH 1/2] Fix AuthLogin Bug (token name) --- client/src/auth/auth_utils.ts | 51 ++++++++----------- .../src/modules/Profile/Component/Profile.tsx | 6 +-- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/client/src/auth/auth_utils.ts b/client/src/auth/auth_utils.ts index 6b02f919..b268693a 100644 --- a/client/src/auth/auth_utils.ts +++ b/client/src/auth/auth_utils.ts @@ -69,28 +69,24 @@ export function useAuthMandatoryLogin(redirectFrom: string): { Session.setPersistent({ redirectFrom: redirectFrom }) history.push('/login') } - - const authToken = getAuthToken() - + const authToken = getAuthToken(); async function getEmail() { if (!authToken || authToken === '') { signIn(redirectFrom) } else { - await axios + const response = await axios .post('/api/auth/get-email', { - token: token, - }) - .then((response) => { - if (response.status === 200) { - const userEmail = response.data.result - const userNetId = userEmail.substring(0, userEmail.lastIndexOf('@')) - setNetId(userNetId) - } + token: authToken, }) + if (response.status === 200) { + const email = response.data.result; + const netid = email.substring(0, email.lastIndexOf('@')) + setNetId(netid) + } } } - getEmail().catch((e) => console.log(e.response)) + getEmail().catch((e) => console.log("Failed in auth_util: ", e.response)) setToken(authToken) setIsAuthenticating(false) setIsLoggedIn(true) @@ -127,26 +123,21 @@ export function useAuthOptionalLogin(): { const [netId, setNetId] = useState('') const history = useHistory() - useEffect(() => { - const token = getAuthToken() - - if (token && token !== '') { - axios - .post('/api/auth/get-email', { - token: token, - }) - .then((response) => { - if (response.status === 200) { - const verifiedEmail = response.data.result - setNetId(verifiedEmail.substring(0, verifiedEmail.lastIndexOf('@'))) - } - }) - .catch((e) => console.log(e.response)) - - setToken(token) + const authToken = getAuthToken(); + async function getEmail() { + if (authToken && authToken !== '') { + const response = await axios.post('/api/auth/get-email'); + if (response.status === 200) { + const email = response.data.result; + setNetId(email.substring(0, email.lastIndexOf('@'))); + } + } + setToken(authToken) setIsLoggedIn(true) } + getEmail().catch(e => console.log('[ERROR] Get Email in useAuthOptionalLogin(): ', e)); + }, []) const signIn = (redirectFrom: string) => { diff --git a/client/src/modules/Profile/Component/Profile.tsx b/client/src/modules/Profile/Component/Profile.tsx index 1f41d2fe..f1171032 100644 --- a/client/src/modules/Profile/Component/Profile.tsx +++ b/client/src/modules/Profile/Component/Profile.tsx @@ -28,7 +28,7 @@ const Profile = () => { const [reviewsTotal, setReviewsTotal] = useState('0') const [reviewsHelpful, setReviewsHelpful] = useState('0') - const {isLoggedIn, token, netId, isAuthenticating, signOut} = + const { isLoggedIn, token, netId, isAuthenticating, signOut } = useAuthMandatoryLogin('profile') const profilePicture: string = randomPicture(netId) @@ -79,7 +79,7 @@ const Profile = () => { useEffect(() => { async function getReviews() { - const response = await axios.post(`/api/profiles/get-reviews`, {netId}) + const response = await axios.post(`/api/profiles/get-reviews`, { netId }) const reviews = response.data.result const pendingReviews = reviews.filter(function (review: ReviewType) { @@ -95,7 +95,7 @@ const Profile = () => { setPastReviews(pastReviews) setLoading(false) } - + getReviews() }, [netId]) From 334cc0388599b30908f874f51b0bdfe153f73df2 Mon Sep 17 00:00:00 2001 From: Will Zhang Date: Thu, 10 Oct 2024 13:39:15 -0400 Subject: [PATCH 2/2] Update error message. --- client/src/auth/auth_utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/auth/auth_utils.ts b/client/src/auth/auth_utils.ts index b268693a..feeece7f 100644 --- a/client/src/auth/auth_utils.ts +++ b/client/src/auth/auth_utils.ts @@ -86,7 +86,7 @@ export function useAuthMandatoryLogin(redirectFrom: string): { } } - getEmail().catch((e) => console.log("Failed in auth_util: ", e.response)) + getEmail().catch((e) => console.log("[ERROR] Failed in authMandatoryLogin: ", e.response)) setToken(authToken) setIsAuthenticating(false) setIsLoggedIn(true)