mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-03 17:53:14 +00:00
54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
import { useEffect, useMemo, type ReactNode } from "react";
|
|
import * as Sentry from "@sentry/react";
|
|
import { useQueryClient } from "@tanstack/react-query";
|
|
import { useChangePassword } from "@/domain/auth/use-cases/use-change-password";
|
|
import { useLogin, useLogout, useSession } from "@/domain/auth/use-cases/use-login";
|
|
import { startQueryBroadcast } from "@/lib/query/setup-query-broadcast";
|
|
import { AuthContext, type AuthContextValue } from "@/providers/auth-context";
|
|
|
|
export function AuthProvider({ children }: { children: ReactNode }) {
|
|
const queryClient = useQueryClient();
|
|
const sessionQuery = useSession();
|
|
const loginMutation = useLogin();
|
|
const logoutMutation = useLogout();
|
|
const changePasswordMutation = useChangePassword();
|
|
|
|
const userId = sessionQuery.data?.id;
|
|
|
|
useEffect(() => {
|
|
Sentry.setUser(userId == null ? null : { id: String(userId) });
|
|
}, [userId]);
|
|
|
|
useEffect(() => {
|
|
if (userId == null) {
|
|
return;
|
|
}
|
|
startQueryBroadcast(queryClient, userId);
|
|
}, [queryClient, userId]);
|
|
|
|
const value = useMemo<AuthContextValue>(
|
|
() => ({
|
|
user: sessionQuery.data,
|
|
isAuthenticated: Boolean(sessionQuery.data),
|
|
isLoading: sessionQuery.isLoading,
|
|
error: sessionQuery.error,
|
|
login: (credentials) => loginMutation.mutate(credentials),
|
|
logout: () => logoutMutation.mutate(),
|
|
changePassword: (data) => changePasswordMutation.mutate(data),
|
|
isLoggingIn: loginMutation.isPending,
|
|
isLoggingOut: logoutMutation.isPending,
|
|
isChangingPassword: changePasswordMutation.isPending,
|
|
loginError: loginMutation.error,
|
|
}),
|
|
[
|
|
sessionQuery.data,
|
|
sessionQuery.isLoading,
|
|
sessionQuery.error,
|
|
loginMutation,
|
|
logoutMutation,
|
|
changePasswordMutation,
|
|
],
|
|
);
|
|
|
|
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
|
}
|