Skip to content

Commit

Permalink
Merge branch 'master' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
ani-kalpachka committed Jul 27, 2023
2 parents 0cb5d0b + f8d8df6 commit d621e73
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/common/util/useCurrentPerson.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AxiosResponse } from 'axios'
import { AxiosError, AxiosResponse } from 'axios'
import { useQuery } from '@tanstack/react-query'
import { useSession } from 'next-auth/react'
import { signOut, useSession } from 'next-auth/react'

import { apiClient } from 'service/apiClient'
import { endpoints } from 'service/apiEndpoints'
Expand All @@ -15,20 +15,45 @@ type CurrentPerson = {
status: 'unauthenticated'
}

//Note: Used only for /profile page
export function getCurrentPerson(isNew = false) {
const { data: session } = useSession()
return useQuery<CurrentPerson>(
const query = useQuery<CurrentPerson, AxiosError>(
[isNew ? endpoints.account.new.url : endpoints.account.me.url],
authQueryFnFactory<CurrentPerson>(session?.accessToken),
{
retry: (count, err) => {
if (err.isAxiosError && err.response?.status === 401) {
return false
}
return true
},
},
)
if (query.error && query.error.response?.status === 401)
signOut({ redirect: true, callbackUrl: '/login' })

return query
}

//Note: Used for every other page
export function useCurrentPerson() {
const { data: session } = useSession()
return useQuery<CurrentPerson>(
const query = useQuery<CurrentPerson, AxiosError>(
[endpoints.account.me.url],
authQueryFnFactory<CurrentPerson>(session?.accessToken),
{
retry: (count, err) => {
if (err.isAxiosError && err.response?.status === 401) {
return false
}
return true
},
},
)
if (query.error && query.error.response?.status === 401) signOut({ redirect: false })

return query
}

export function updateCurrentPerson() {
Expand Down
12 changes: 12 additions & 0 deletions src/components/client/auth/profile/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { routes } from 'common/routes'
import Layout from 'components/client/layout/Layout'

import { ProfileTabs, ProfileTab, tabs } from './tabs'
import { getCurrentPerson } from 'common/util/useCurrentPerson'

const PREFIX = 'ProfilePage'

Expand Down Expand Up @@ -50,6 +51,9 @@ export default function ProfilePage() {
const router = useRouter()
const matches = useMediaQuery(theme.breakpoints.down('sm'))
const currentTab = router.query.slug ?? ProfileTabs.donations

const { error: userError, isError } = getCurrentPerson(!!router.query?.register)

const tab = useMemo<ProfileTab>(() => {
return tabs.find((tab) => tab.slug === currentTab) ?? tabs[0]
}, [currentTab])
Expand All @@ -62,6 +66,14 @@ export default function ProfilePage() {
return <StyledLayout title={t('nav.profile')}>Not authenticated</StyledLayout>
}

if (isError && userError.response && userError.response.status === 401) {
return (
<StyledLayout title={t('nav.profile')}>
The user session has expired. Redirecting to login page
</StyledLayout>
)
}

const { Component: SelectedTab } = tab

return (
Expand Down

0 comments on commit d621e73

Please sign in to comment.