mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 23:15:36 +00:00
feat: update default layout
This commit is contained in:
25
inertia/hooks/use_auth.tsx
Normal file
25
inertia/hooks/use_auth.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import { usePage } from '@inertiajs/react';
|
||||
import type { Auth, InertiaPage } from '~/types/inertia';
|
||||
|
||||
export const useAuth = () => usePage<InertiaPage>().props.auth;
|
||||
|
||||
export const withAuth = <T extends object>(
|
||||
Component: React.ComponentType<T & { auth: Auth }>
|
||||
): React.ComponentType<Omit<T, 'auth'>> => {
|
||||
return (props: Omit<T, 'auth'>) => {
|
||||
const auth = useAuth();
|
||||
return <Component {...(props as T)} auth={auth} />;
|
||||
};
|
||||
};
|
||||
|
||||
export const withAuthRequired = <T extends object>(
|
||||
Component: React.ComponentType<T & { auth: Auth }>
|
||||
): React.ComponentType<Omit<T, 'auth'>> => {
|
||||
return (props: Omit<T, 'auth'>) => {
|
||||
const auth = useAuth();
|
||||
if (!auth.isAuthenticated) {
|
||||
return null;
|
||||
}
|
||||
return <Component {...(props as T)} auth={auth} />;
|
||||
};
|
||||
};
|
||||
@@ -1,5 +0,0 @@
|
||||
import { usePage } from '@inertiajs/react';
|
||||
import type { InertiaPage } from '~/types/inertia';
|
||||
|
||||
const useUser = () => usePage<InertiaPage>().props.auth;
|
||||
export default useUser;
|
||||
Reference in New Issue
Block a user