login page
This commit is contained in:
43
frontend/components/Logout.tsx
Normal file
43
frontend/components/Logout.tsx
Normal file
@@ -0,0 +1,43 @@
|
||||
import React from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { signOut } from 'next-auth/react'
|
||||
import Button from './ui/Button'
|
||||
import { IoExitOutline } from 'react-icons/io5'
|
||||
import useUserStore from '@/app/store/userStore'
|
||||
|
||||
const Logout = () => {
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL
|
||||
const router = useRouter()
|
||||
|
||||
const handleLogout = async () => {
|
||||
try {
|
||||
// бекенд чистит куки
|
||||
await fetch(`${API_URL}/logout`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
})
|
||||
|
||||
// чистим стор
|
||||
useUserStore.getState().logout()
|
||||
|
||||
// если логин был гугловый
|
||||
await signOut({ redirect: false })
|
||||
|
||||
// редирект
|
||||
router.push('/')
|
||||
router.refresh() // обновляем страницу чтобы обновить стейты
|
||||
} catch (error) {
|
||||
console.error('Logout error:', error)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Button
|
||||
text="Выйти из аккаунта"
|
||||
className="w-full text-sm font-medium text-gray-600 hover:bg-gray-100 px-4 py-4 flex items-center rounded-lg transition-colors"
|
||||
onClick={handleLogout}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export default Logout
|
||||
Reference in New Issue
Block a user