From a1b9bfcba047c658ff063b002eca3ca54438ff44 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Thu, 30 Jan 2025 11:24:19 +0100 Subject: [PATCH] fix: add login event actions --- frontend/src/contexts/auth.context.tsx | 4 ++++ frontend/src/contexts/broadcast-channel.context.tsx | 1 + frontend/src/hooks/entities/auth-hooks.ts | 5 +++++ 3 files changed, 10 insertions(+) diff --git a/frontend/src/contexts/auth.context.tsx b/frontend/src/contexts/auth.context.tsx index 2d238f62..99978f15 100644 --- a/frontend/src/contexts/auth.context.tsx +++ b/frontend/src/contexts/auth.context.tsx @@ -101,6 +101,10 @@ export const AuthProvider = ({ children }: AuthProviderProps): JSX.Element => { }; const isAuthenticated = !!user; + useSubscribeBroadcastChannel("login", () => { + router.reload(); + }); + useSubscribeBroadcastChannel("logout", () => { router.reload(); }); diff --git a/frontend/src/contexts/broadcast-channel.context.tsx b/frontend/src/contexts/broadcast-channel.context.tsx index 330db77f..a0046bf8 100644 --- a/frontend/src/contexts/broadcast-channel.context.tsx +++ b/frontend/src/contexts/broadcast-channel.context.tsx @@ -18,6 +18,7 @@ import { import { generateId } from "@/utils/generateId"; export enum EBCEvent { + LOGIN = "login", LOGOUT = "logout", } diff --git a/frontend/src/hooks/entities/auth-hooks.ts b/frontend/src/hooks/entities/auth-hooks.ts index e6df15b3..2e829206 100755 --- a/frontend/src/hooks/entities/auth-hooks.ts +++ b/frontend/src/hooks/entities/auth-hooks.ts @@ -33,12 +33,17 @@ export const useLogin = ( >, ) => { const { apiClient } = useApiClient(); + const { postMessage } = useBroadcastChannel(); return useMutation({ ...options, async mutationFn(credentials) { return await apiClient.login(credentials); }, + onSuccess: (data, variables, context) => { + options?.onSuccess?.(data, variables, context); + postMessage({ event: "login" }); + }, }); };