Commit 7da21afd authored by Stefan Probst's avatar Stefan Probst
Browse files

fix: ensure get user data is authenticated

parent 79a078be
Pipeline #210143 canceled with stages
in 1 minute and 40 seconds
......@@ -8,7 +8,8 @@ import type { UseMutationOptions, UseMutationResult } from 'react-query'
import { useMutation } from 'react-query'
import type { ImplicitGrantTokenData, OAuthRegistrationDto } from '@/api/sshoc'
import { baseUrl, request } from '@/api/sshoc'
import { baseUrl, request, useGetLoggedInUser } from '@/api/sshoc'
import { useAuth } from '@/modules/auth/AuthContext'
/**
* Sign in user with username and password.
......@@ -191,3 +192,23 @@ export function getMediaFileUrl({ mediaId }: { mediaId: string }): string {
)
return String(url)
}
export function useCurrentUser(): ReturnType<typeof useGetLoggedInUser> {
const auth = useAuth()
return useGetLoggedInUser(
{
enabled: auth.session?.accessToken !== undefined,
/** immediately sign out in case of error */
retry: false,
/** cache until cache is cleared manually */
staleTime: Infinity,
onError() {
auth.signOut()
},
},
{
token: auth.session?.accessToken,
},
)
}
......@@ -2,7 +2,8 @@ import { useRouter } from 'next/router'
import { useQueryClient } from 'react-query'
import type { DatasetCore, DatasetDto } from '@/api/sshoc'
import { useCreateDataset, useGetLoggedInUser } from '@/api/sshoc'
import { useCreateDataset } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import type { ItemCategory } from '@/api/sshoc/types'
import { ActorsFormSection } from '@/components/item/ActorsFormSection/ActorsFormSection'
import { DateFormSection } from '@/components/item/DateFormSection/DateFormSection'
......@@ -43,7 +44,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const toast = useToast()
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const user = useCurrentUser()
const handleErrors = useErrorHandlers()
const validateCommonFormFields = useValidateCommonFormFields()
const isAllowedToPublish =
......
......@@ -2,7 +2,8 @@ import { useRouter } from 'next/router'
import { useQueryClient } from 'react-query'
import type { DatasetCore, DatasetDto } from '@/api/sshoc'
import { useGetLoggedInUser, useUpdateDataset } from '@/api/sshoc'
import { useUpdateDataset } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import type { ItemCategory } from '@/api/sshoc/types'
import { ActorsFormSection } from '@/components/item/ActorsFormSection/ActorsFormSection'
import { DateFormSection } from '@/components/item/DateFormSection/DateFormSection'
......@@ -48,7 +49,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const toast = useToast()
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const user = useCurrentUser()
const handleErrors = useErrorHandlers()
const validateCommonFormFields = useValidateCommonFormFields()
const isAllowedToPublish =
......
......@@ -2,7 +2,8 @@ import { useRouter } from 'next/router'
import { useQueryClient } from 'react-query'
import type { PublicationCore, PublicationDto } from '@/api/sshoc'
import { useCreatePublication, useGetLoggedInUser } from '@/api/sshoc'
import { useCreatePublication } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import type { ItemCategory } from '@/api/sshoc/types'
import { ActorsFormSection } from '@/components/item/ActorsFormSection/ActorsFormSection'
import { DateFormSection } from '@/components/item/DateFormSection/DateFormSection'
......@@ -43,7 +44,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const toast = useToast()
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const user = useCurrentUser()
const handleErrors = useErrorHandlers()
const validateCommonFormFields = useValidateCommonFormFields()
const isAllowedToPublish =
......
......@@ -2,7 +2,8 @@ import { useRouter } from 'next/router'
import { useQueryClient } from 'react-query'
import type { PublicationCore, PublicationDto } from '@/api/sshoc'
import { useGetLoggedInUser, useUpdatePublication } from '@/api/sshoc'
import { useUpdatePublication } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import type { ItemCategory } from '@/api/sshoc/types'
import { ActorsFormSection } from '@/components/item/ActorsFormSection/ActorsFormSection'
import { DateFormSection } from '@/components/item/DateFormSection/DateFormSection'
......@@ -48,7 +49,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const toast = useToast()
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const user = useCurrentUser()
const handleErrors = useErrorHandlers()
const validateCommonFormFields = useValidateCommonFormFields()
const isAllowedToPublish =
......
......@@ -2,7 +2,8 @@ import { useRouter } from 'next/router'
import { useQueryClient } from 'react-query'
import type { ToolCore, ToolDto } from '@/api/sshoc'
import { useCreateTool, useGetLoggedInUser } from '@/api/sshoc'
import { useCreateTool } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import type { ItemCategory } from '@/api/sshoc/types'
import { ActorsFormSection } from '@/components/item/ActorsFormSection/ActorsFormSection'
import { MainFormSection } from '@/components/item/MainFormSection/MainFormSection'
......@@ -41,7 +42,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const toast = useToast()
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const user = useCurrentUser()
const handleErrors = useErrorHandlers()
const validateCommonFormFields = useValidateCommonFormFields()
const isAllowedToPublish =
......
......@@ -2,7 +2,8 @@ import { useRouter } from 'next/router'
import { useQueryClient } from 'react-query'
import type { ToolCore, ToolDto } from '@/api/sshoc'
import { useGetLoggedInUser, useUpdateTool } from '@/api/sshoc'
import { useUpdateTool } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import type { ItemCategory } from '@/api/sshoc/types'
import { ActorsFormSection } from '@/components/item/ActorsFormSection/ActorsFormSection'
import { MainFormSection } from '@/components/item/MainFormSection/MainFormSection'
......@@ -46,7 +47,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const toast = useToast()
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const user = useCurrentUser()
const handleErrors = useErrorHandlers()
const validateCommonFormFields = useValidateCommonFormFields()
const isAllowedToPublish =
......
......@@ -2,7 +2,8 @@ import { useRouter } from 'next/router'
import { useQueryClient } from 'react-query'
import type { TrainingMaterialCore, TrainingMaterialDto } from '@/api/sshoc'
import { useCreateTrainingMaterial, useGetLoggedInUser } from '@/api/sshoc'
import { useCreateTrainingMaterial } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import type { ItemCategory } from '@/api/sshoc/types'
import { ActorsFormSection } from '@/components/item/ActorsFormSection/ActorsFormSection'
import { MainFormSection } from '@/components/item/MainFormSection/MainFormSection'
......@@ -41,7 +42,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const toast = useToast()
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const user = useCurrentUser()
const handleErrors = useErrorHandlers()
const validateCommonFormFields = useValidateCommonFormFields()
const isAllowedToPublish =
......
......@@ -2,7 +2,8 @@ import { useRouter } from 'next/router'
import { useQueryClient } from 'react-query'
import type { TrainingMaterialCore, TrainingMaterialDto } from '@/api/sshoc'
import { useGetLoggedInUser, useUpdateTrainingMaterial } from '@/api/sshoc'
import { useUpdateTrainingMaterial } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import type { ItemCategory } from '@/api/sshoc/types'
import { ActorsFormSection } from '@/components/item/ActorsFormSection/ActorsFormSection'
import { MainFormSection } from '@/components/item/MainFormSection/MainFormSection'
......@@ -46,7 +47,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const toast = useToast()
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const user = useCurrentUser()
const handleErrors = useErrorHandlers()
const validateCommonFormFields = useValidateCommonFormFields()
const isAllowedToPublish =
......
......@@ -10,11 +10,8 @@ import { Fragment, useEffect, useRef, useState } from 'react'
import { useQueryClient } from 'react-query'
import type { StepCore, WorkflowCore, WorkflowDto } from '@/api/sshoc'
import {
useCreateStep,
useCreateWorkflow,
useGetLoggedInUser,
} from '@/api/sshoc'
import { useCreateStep, useCreateWorkflow } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import type { ItemCategory } from '@/api/sshoc/types'
import { ActorsFormSection } from '@/components/item/ActorsFormSection/ActorsFormSection'
import { MainFormSection } from '@/components/item/MainFormSection/MainFormSection'
......@@ -59,7 +56,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const toast = useToast()
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const user = useCurrentUser()
const handleErrors = useErrorHandlers()
const validateCommonFormFields = useValidateCommonFormFields()
const isAllowedToPublish =
......
......@@ -10,12 +10,8 @@ import { Fragment, useEffect, useRef, useState } from 'react'
import { useQueryClient } from 'react-query'
import type { StepCore, WorkflowCore, WorkflowDto } from '@/api/sshoc'
import {
useCreateStep,
useGetLoggedInUser,
useUpdateStep,
useUpdateWorkflow,
} from '@/api/sshoc'
import { useCreateStep, useUpdateStep, useUpdateWorkflow } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import type { ItemCategory } from '@/api/sshoc/types'
import { ActorsFormSection } from '@/components/item/ActorsFormSection/ActorsFormSection'
import { MainFormSection } from '@/components/item/MainFormSection/MainFormSection'
......@@ -65,7 +61,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
const toast = useToast()
const router = useRouter()
const auth = useAuth()
const user = useGetLoggedInUser()
const user = useCurrentUser()
const handleErrors = useErrorHandlers()
const validateCommonFormFields = useValidateCommonFormFields()
const isAllowedToPublish =
......
......@@ -3,7 +3,7 @@ import type { PropsWithChildren } from 'react'
import { Fragment, useEffect } from 'react'
import type { UserDto } from '@/api/sshoc'
import { useGetLoggedInUser } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import { useAuth } from '@/modules/auth/AuthContext'
export default function ProtectedScreen({
......@@ -14,10 +14,7 @@ export default function ProtectedScreen({
}>): JSX.Element | null {
const router = useRouter()
const { session } = useAuth()
const user = useGetLoggedInUser(
{ enabled: session?.accessToken != null },
{ token: session?.accessToken },
)
const user = useCurrentUser()
useEffect(() => {
if (session === null || !hasAppropriateRole(roles, user.data)) {
......
import type { PropsWithChildren } from 'react'
import { Fragment } from 'react'
import { useGetLoggedInUser } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import { useAuth } from '@/modules/auth/AuthContext'
export default function ProtectedView({
......@@ -11,10 +11,7 @@ export default function ProtectedView({
roles?: Array<'contributor' | 'moderator' | 'administrator'>
}>): JSX.Element | null {
const { session } = useAuth()
const user = useGetLoggedInUser(
{ enabled: session?.accessToken != null },
{ token: session?.accessToken },
)
const user = useCurrentUser()
if (session === null) return null
......
......@@ -6,7 +6,8 @@ import { useRouter } from 'next/router'
import type { PropsWithChildren, Ref } from 'react'
import { Fragment, useEffect, useState } from 'react'
import { useGetItemCategories, useGetLoggedInUser } from '@/api/sshoc'
import { useGetItemCategories } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import type { ItemCategory, ItemSearchQuery } from '@/api/sshoc/types'
import { useAuth } from '@/modules/auth/AuthContext'
import ProtectedView from '@/modules/auth/ProtectedView'
......@@ -23,7 +24,6 @@ import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLab
import type { UrlObject } from '@/utils/useActiveLink'
import { useActiveLink } from '@/utils/useActiveLink'
import { Svg as Logo } from '@@/assets/images/logo-with-text.svg'
/**
* Page header.
*/
......@@ -243,19 +243,7 @@ function AuthButton() {
const router = useRouter()
const auth = useAuth()
const { data: user } = useGetLoggedInUser(
{
enabled: auth.session?.accessToken !== undefined,
/** immediately sign out in case of error */
retry: false,
onError() {
auth.signOut()
},
},
{
token: auth.session?.accessToken,
},
)
const { data: user } = useCurrentUser()
const [redirectPath, setRedirectPath] = useState(() => {
const path = getRedirectPath(router.asPath)
if (path === undefined) return undefined
......
import Link from 'next/link'
import { Fragment } from 'react'
import { useGetLoggedInUser } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import { Icon } from '@/elements/Icon/Icon'
import { Svg as ActorsIcon } from '@/elements/icons/big/actors.svg'
import { Svg as ContributedItemsIcon } from '@/elements/icons/big/contributed-items.svg'
......@@ -57,7 +57,7 @@ const fields = [
* My account screen.
*/
export default function AccountScreen(): JSX.Element {
const user = useGetLoggedInUser()
const user = useCurrentUser()
// just for typescript - user should always exist here
if (user.data == null) return <div>User not found</div>
......
......@@ -6,7 +6,8 @@ import type { ChangeEvent, FormEvent, Key } from 'react'
import { Fragment, useEffect, useState } from 'react'
import type { SearchItem, SearchItems } from '@/api/sshoc'
import { useGetLoggedInUser, useSearchItems } from '@/api/sshoc'
import { useSearchItems } from '@/api/sshoc'
import { useCurrentUser } from '@/api/sshoc/client'
import type { ItemCategory, ItemSearchQuery } from '@/api/sshoc/types'
import { ProgressSpinner } from '@/elements/ProgressSpinner/ProgressSpinner'
import { Select } from '@/elements/Select/Select'
......@@ -39,7 +40,7 @@ export default function ContributedItemsScreen(): JSX.Element {
const router = useRouter()
const query = sanitizeQuery(router.query)
const user = useGetLoggedInUser()
const user = useCurrentUser()
const auth = useAuth()
const handleErrors = useErrorHandlers()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment