wacrm/src/components/settings/sessions-card.tsx
Arnas Donauskas 4bb157c705
Merge remote-tracking branch 'origin/main' into chore/hostinger-accent-palette
# Conflicts:
#	src/app/icon.tsx
#	src/components/docs/shell.tsx
#	src/components/landing/footer.tsx
#	src/components/landing/nav.tsx
2026-05-06 13:35:46 +03:00

110 lines
3.1 KiB
TypeScript

'use client';
import { useState } from 'react';
import { toast } from 'sonner';
import { Loader2, LogOut } from 'lucide-react';
import { createClient } from '@/lib/supabase/client';
import { Button } from '@/components/ui/button';
import {
Card,
CardContent,
CardHeader,
CardTitle,
CardDescription,
} from '@/components/ui/card';
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
DialogFooter,
} from '@/components/ui/dialog';
export function SessionsCard() {
const supabase = createClient();
const [open, setOpen] = useState(false);
const [signingOut, setSigningOut] = useState(false);
const onConfirm = async () => {
setSigningOut(true);
try {
// scope: 'global' revokes every refresh token for this user
// across all devices; the next auth-state change on this tab
// triggers the usual redirect.
const { error } = await supabase.auth.signOut({ scope: 'global' });
if (error) {
toast.error(`Sign-out failed: ${error.message}`);
return;
}
window.location.href = '/login';
} catch (err) {
const msg = err instanceof Error ? err.message : 'Unknown error';
toast.error(msg);
} finally {
setSigningOut(false);
}
};
return (
<>
<Card className="bg-slate-900/40 border-slate-800">
<CardHeader>
<CardTitle className="flex items-center gap-2 text-white">
<LogOut className="size-4 text-violet-400" />
Active sessions
</CardTitle>
<CardDescription className="text-slate-400">
Sign out of every device where you&apos;re logged in — including
this one. Useful if you lost a laptop or shared your password.
</CardDescription>
</CardHeader>
<CardContent>
<Button
type="button"
variant="outline"
onClick={() => setOpen(true)}
>
<LogOut className="size-4" />
Sign out of all devices
</Button>
</CardContent>
</Card>
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Sign out everywhere?</DialogTitle>
<DialogDescription>
Every device logged into this account will be signed out and
will need to log in again. You will be redirected to the login
page.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button
type="button"
variant="ghost"
onClick={() => setOpen(false)}
disabled={signingOut}
>
Cancel
</Button>
<Button type="button" onClick={onConfirm} disabled={signingOut}>
{signingOut ? (
<>
<Loader2 className="size-4 animate-spin" />
Signing out…
</>
) : (
'Sign out everywhere'
)}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>
);
}