import { Maybe } from '@src/generated/graphql' ; import { useViewer } from '@src/modules/auth/ViewerContext' ; import { ID } from '@src/modules/shared/SharedTypes' ; import { urls } from '@src/modules/routing/RoutingUtils' ; import { useRouter } from 'next/router' ; import { useCallback } from 'react' ; export const useRequireAuth = ( ) => { const viewer = useViewer ( ) ; const requireAuth = useCallback ( ( authenticated , fallback = null ) => { if ( ! viewer ) { return fallback ; } return authenticated ; } , [ viewer ] ) ; return requireAuth ; } ; export const useRequireOwner = ( ownerId ) => { const viewer = useViewer ( ) ; const requireOwner = useCallback ( ( matched , fallback = null ) => { if ( ! viewer || ! ownerId || viewer . id !== ownerId ) { return fallback ; } return matched ; } , [ ownerId , viewer ] ) ; return requireOwner ; } ; interface UsePrivateActionArgs { isAuthRequired ? : boolean ; action : Maybe < Function > ; } export const usePrivateAction = ( { isAuthRequired , action , } ) => { const router = useRouter ( ) ; const requireAuth = useRequireAuth ( ) ; if ( ! isAuthRequired ) { return action ; } return requireAuth ( action , ( ) => router . push ( urls . login ( ) ) ) ; } ;