Skip to content

Commit

Permalink
Merge pull request #78 from Benjtalkshow/logout-functionality
Browse files Browse the repository at this point in the history
feat: implement logout functionality
  • Loading branch information
Benjtalkshow authored Feb 19, 2025
2 parents 1703f5f + 3307658 commit faacc57
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 12 additions & 3 deletions shared/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { useSearch } from '@/hooks/useSearch';
import { NotificationList } from './NotificationList';
import { UserNav } from './UserNav';
import { useNotificationStore } from '@/store/notificationStore';
import { useAuthStore } from '@/store/authStore';
import { useRouter } from 'next/navigation';

interface SearchBarProps {
searchFunction: (query: string) => Promise<any[]>;
Expand Down Expand Up @@ -38,10 +40,17 @@ export default function SearchBar({
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
};
const logout = useAuthStore((state) => state.logout);
const router = useRouter();

function handleLogout(): void {
console.log('Logout clicked');
}
const handleLogout = async () => {
try {
await logout();
router.push('/auth/login');
} catch (error) {
console.error('Logout failed:', error);
}
};

return (
<form onSubmit={handleSubmit} className={`flex items-center gap-4 lg:gap-6 ${className}`}>
Expand Down
6 changes: 3 additions & 3 deletions store/authStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ export const useAuthStore = create<AuthState>((set) => ({
const response: Response = await fetch('/api/auth/logout', {
method: 'POST',
});

if (!response.ok) throw new Error('Logout failed');

set({ user: null });
Cookies.remove('token');
set({ user: null });
} catch (error) {
console.error('Logout error:', error);
throw error;
Expand Down

0 comments on commit faacc57

Please sign in to comment.