Skip to content

Commit

Permalink
Merge pull request #462 from cornell-dti/wz/profilereviewsbug-misc
Browse files Browse the repository at this point in the history
  • Loading branch information
wizhaaa authored Oct 10, 2024
2 parents c4b8604 + 334cc03 commit a490f12
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 33 deletions.
51 changes: 21 additions & 30 deletions client/src/auth/auth_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("[ERROR] Failed in authMandatoryLogin: ", e.response))
setToken(authToken)
setIsAuthenticating(false)
setIsLoggedIn(true)
Expand Down Expand Up @@ -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) => {
Expand Down
6 changes: 3 additions & 3 deletions client/src/modules/Profile/Component/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Check warning on line 84 in client/src/modules/Profile/Component/Profile.tsx

View workflow job for this annotation

GitHub Actions / build

'reviews' is already declared in the upper scope on line 24 column 10
const pendingReviews = reviews.filter(function (review: ReviewType) {

Check warning on line 85 in client/src/modules/Profile/Component/Profile.tsx

View workflow job for this annotation

GitHub Actions / build

'pendingReviews' is already declared in the upper scope on line 25 column 10
Expand All @@ -95,7 +95,7 @@ const Profile = () => {
setPastReviews(pastReviews)
setLoading(false)
}

getReviews()
}, [netId])

Expand Down

0 comments on commit a490f12

Please sign in to comment.