File size: 2,206 Bytes
c7fbc2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"use client";

import { RefreshCw, CheckCircle, AlertCircle } from "lucide-react";
import { useState } from "react";
import { useRouter } from "next/navigation";

export default function RefreshButton() {
    const [isLoading, setIsLoading] = useState(false);
    const [status, setStatus] = useState<"idle" | "success" | "error">("idle");
    const router = useRouter();

    const handleRefresh = async () => {
        setIsLoading(true);
        setStatus("idle");
        try {
            const res = await fetch("/api/auth/refresh", { method: "POST" });
            const data = await res.json();

            if (res.ok) {
                setStatus("success");
                setTimeout(() => setStatus("idle"), 3000);
                router.refresh();
            } else {
                console.error(data);
                setStatus("error");
            }
        } catch (error) {
            console.error(error);
            setStatus("error");
        } finally {
            setIsLoading(false);
        }
    };

    return (
        <div className="flex items-center gap-2">
            {status === "success" && (
                <span className="text-sm text-green-600 flex items-center gap-1 animate-in fade-in slide-in-from-right-2">
                    <CheckCircle size={14} /> Refreshed
                </span>
            )}
            {status === "error" && (
                <span className="text-sm text-red-600 flex items-center gap-1 animate-in fade-in slide-in-from-right-2">
                    <AlertCircle size={14} /> Failed
                </span>
            )}
            <button
                onClick={handleRefresh}
                disabled={isLoading}
                className="px-4 py-2 bg-white dark:bg-zinc-800 border border-gray-200 dark:border-zinc-700 hover:bg-gray-50 dark:hover:bg-zinc-700 text-gray-700 dark:text-gray-200 rounded-lg text-sm font-medium transition-all flex items-center gap-2 disabled:opacity-50 disabled:cursor-not-allowed"
            >
                <RefreshCw size={16} className={isLoading ? "animate-spin" : ""} />
                {isLoading ? "Refreshing..." : "Refresh Token"}
            </button>
        </div>
    );
}