summaryrefslogtreecommitdiff
path: root/src/components/settings/LogoutButton.tsx
blob: 22cbb27eb4f781b5d5eebebed43fbc866ebb83c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'use client';

import { Button } from '@/components/ui/button';
import { useAuthContext } from '@/lib/contexts/Auth.context';
import { useRouter } from 'next/navigation';
import { useState } from 'react';

export default function LogoutButton() {
    const { logout } = useAuthContext();
    const router = useRouter();
    const [loading, setLoading] = useState(false);

    const handleLogout = async () => {
        if (loading) return;

        setLoading(true);

        try {
            await logout();
            router.push('/');
        } finally {
            setLoading(false);
        }
    };

    return (
        <Button className="w-[310px]" onClick={handleLogout} disabled={loading}>
            {loading ? 'Выход...' : 'Выйти из аккаунта'}
        </Button>
    );
}