mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-28 20:55:42 +00:00
* wip: add middlewares * refactor: use context fom middleware in handlers * refactor: use controller approach in handlers * refactor: move oauth providers into services (non-working) * feat: create oauth broker service * refactor: use a boostrap service to bootstrap the app * refactor: split utils into smaller files * refactor: use more clear name for frontend assets * feat: allow customizability of resources dir * fix: fix typo in ui middleware * fix: validate resource file paths in ui middleware * refactor: move resource handling to a controller * feat: add some logging * fix: configure middlewares before groups * fix: use correct api path in login mutation * fix: coderabbit suggestions * fix: further coderabbit suggestions
49 lines
1.0 KiB
TypeScript
49 lines
1.0 KiB
TypeScript
import {
|
|
userContextSchema,
|
|
UserContextSchema,
|
|
} from "@/schemas/user-context-schema";
|
|
import { createContext, useContext } from "react";
|
|
import { useSuspenseQuery } from "@tanstack/react-query";
|
|
import axios from "axios";
|
|
|
|
const UserContext = createContext<UserContextSchema | null>(null);
|
|
|
|
export const UserContextProvider = ({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode;
|
|
}) => {
|
|
const { isFetching, data, error } = useSuspenseQuery({
|
|
queryKey: ["user"],
|
|
queryFn: () => axios.get("/api/context/user").then((res) => res.data),
|
|
});
|
|
|
|
if (error && !isFetching) {
|
|
throw error;
|
|
}
|
|
|
|
const validated = userContextSchema.safeParse(data);
|
|
|
|
if (validated.success === false) {
|
|
throw validated.error;
|
|
}
|
|
|
|
return (
|
|
<UserContext.Provider value={validated.data}>
|
|
{children}
|
|
</UserContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useUserContext = () => {
|
|
const context = useContext(UserContext);
|
|
|
|
if (!context) {
|
|
throw new Error(
|
|
"useUserContext must be used within an UserContextProvider",
|
|
);
|
|
}
|
|
|
|
return context;
|
|
};
|