This commit is contained in:
@@ -5,23 +5,19 @@ import type { UserLogged } from "../resources/users";
|
||||
export type Auth = {
|
||||
loggedUser: UserLogged | null;
|
||||
isLoading: boolean;
|
||||
}
|
||||
};
|
||||
|
||||
const AuthContext = createContext<Auth | undefined>(undefined)
|
||||
const AuthContext = createContext<Auth | undefined>(undefined);
|
||||
|
||||
export function AuthProvider({ children }: {children: React.ReactNode}) {
|
||||
const {data: loggedUser, isLoading} = useCurrentUser();
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const { data: loggedUser, isLoading } = useCurrentUser();
|
||||
|
||||
const value: Auth = {
|
||||
loggedUser: loggedUser ?? null,
|
||||
isLoading,
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={value}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
)
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
}
|
||||
|
||||
export function useAuth(): Auth {
|
||||
@@ -30,4 +26,4 @@ export function useAuth(): Auth {
|
||||
throw new Error("useAuth must be used inside AuthProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { Group, Loader } from "@mantine/core";
|
||||
import { Navigate, Outlet } from "react-router";
|
||||
import { useAuth } from "../AuthProvider";
|
||||
|
||||
export function ProtectedRoute() {
|
||||
const { loggedUser, isLoading } = useAuth();
|
||||
|
||||
if (!loggedUser && isLoading)
|
||||
return (
|
||||
<Group align="center" justify="center" h="80vh" w="100%">
|
||||
<Loader color="pink" />
|
||||
</Group>
|
||||
);
|
||||
|
||||
if (!loggedUser?.logged) {
|
||||
return <Navigate to="/" replace />;
|
||||
}
|
||||
|
||||
return <Outlet />;
|
||||
}
|
||||
import { Group, Loader } from "@mantine/core";
|
||||
import { Navigate, Outlet } from "react-router";
|
||||
import { useAuth } from "../AuthProvider";
|
||||
|
||||
export function ProtectedRoute() {
|
||||
const { loggedUser, isLoading } = useAuth();
|
||||
|
||||
if (!loggedUser && isLoading)
|
||||
return (
|
||||
<Group align="center" justify="center" h="80vh" w="100%">
|
||||
<Loader color="pink" />
|
||||
</Group>
|
||||
);
|
||||
|
||||
if (!loggedUser?.logged) {
|
||||
return <Navigate to="/" replace />;
|
||||
}
|
||||
|
||||
return <Outlet />;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user