diff --git "a/C:\\chatknot\\app\\utils\\handlelogout.js" "b/C:\\chatknot\\old backup\\app\\utils\\handlelogout.js" index cf33160..0abb3de 100644 --- "a/C:\\chatknot\\app\\utils\\handlelogout.js" +++ "b/C:\\chatknot\\old backup\\app\\utils\\handlelogout.js" @@ -1,42 +1,26 @@ // handleLogout.js - A utility function for logout import { reset } from '../lib/features/chat/chatSlice'; import { disconnectSocket } from '../lib/socketManager'; -import { deleteCookie, getCookie } from 'cookies-next'; +import { deleteCookie } from 'cookies-next'; import axios from 'axios'; -// Accept an optional dispatch so callers can invoke this without providing Redux dispatch -const handlelogout = async (dispatch) => { +const handlelogout = async (dispatch) => { // Receive dispatch here try { // Disconnect the socket before logging out disconnectSocket(); - // Call the logout API (best-effort; if cookies are missing the request may fail) - try { - await axios.post( - `${process.env.NEXT_PUBLIC_API_URL}/api/auth/logout`, - {}, - { - headers: { - Authorization: `Bearer ${getCookie('token')}`, - 'x-refresh-token': getCookie('refreshToken'), - }, - } - ); - } catch (err) { - // Ignore errors from the logout endpoint - we'll still clear local state - console.warn('Logout API call failed (continuing to clear local state):', err?.message || err); - } - - // Dispatch the reset action if a dispatch was provided - if (dispatch && typeof dispatch === 'function') { - try { - dispatch(reset()); - } catch (err) { - console.warn('Dispatch reset failed:', err); + // Call the logout API + await axios.post(`${process.env.NEXT_PUBLIC_API_URL}/api/auth/logout`, {}, { + headers: { + Authorization: `Bearer ${getCookie('token')}`, + 'x-refresh-token': getCookie('refreshtoken') } - } + }); + + // Dispatch the reset action + dispatch(reset()); } catch (error) { - console.error('Error during logout flow:', error); + console.error('Error logging out:', error); deleteCookie('token'); deleteCookie('refreshToken'); } finally { @@ -45,9 +29,7 @@ const handlelogout = async (dispatch) => { deleteCookie('refreshToken'); // Redirect to the login page - if (typeof window !== 'undefined') { - window.location.href = '/login'; - } + window.location.href = '/login'; } };