| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | useAuth, |
| 5 | useRevokeOtherSessions, |
| 6 | useRevokeSessions, |
| 7 | } from "@better-auth-ui/react"; |
| 8 | import { LogOut } |
| 9 | import { useState } |
| 10 | import { toast } |
| 11 | |
| 12 | import { |
| 13 | AlertDialog, |
| 14 | AlertDialogCancel, |
| 15 | AlertDialogContent, |
| 16 | AlertDialogDescription, |
| 17 | AlertDialogFooter, |
| 18 | AlertDialogHeader, |
| 19 | AlertDialogMedia, |
| 20 | AlertDialogTitle, |
| 21 | } from "@/components/ui/alert-dialog"; |
| 22 | import { Button } |
| 23 | import { Spinner } |
| 24 | |
| 25 | type PendingAction |
| 26 | |
| 27 | export function SessionActions(props |
| 28 | const { authClient, |
| 29 | useAuth(); |
| 30 | const [action, |
| 31 | |
| 32 | const revokeOtherSessions = |
| 33 | onSuccess: () => |
| 34 | toast.success(localization |
| 35 | setAction(null); |
| 36 | }, |
| 37 | }); |
| 38 | const revokeSessions = |
| 39 | onSuccess: () => |
| 40 | navigate({ |
| 41 | to: `${basePaths. |
| 42 | replace: true, |
| 43 | }), |
| 44 | }); |
| 45 | |
| 46 | const isPending = |
| 47 | const isEverywhere = |
| 48 | |
| 49 | return ( |
| 50 | <> |
| 51 | <div className="flex flex-wrap justify-end gap-2 border-t p-4 |
| 52 | <Button |
| 53 | disabled={!props. |
| 54 | onClick={() => |
| 55 | size="sm" |
| 56 | type="button" |
| 57 | variant="outline" |
| 58 | > |
| 59 | {localization.settings.signOutOtherDevices |
| 60 | </Button> |
| 61 | <Button |
| 62 | disabled={isPending} |
| 63 | onClick={() => |
| 64 | size="sm" |
| 65 | type="button" |
| 66 | variant="destructive" |
| 67 | > |
| 68 | {localization.settings.signOutEverywhere |
| 69 | </Button> |
| 70 | </div> |
| 71 | |
| 72 | <AlertDialog |
| 73 | open={action !== |
| 74 | onOpenChange={(open) |
| 75 | > |
| 76 | <AlertDialogContent> |
| 77 | <AlertDialogHeader> |
| 78 | <AlertDialogMedia className=" |
| 79 | <LogOut /> |
| 80 | </AlertDialogMedia> |
| 81 | <AlertDialogTitle> |
| 82 | {isEverywhere |
| 83 | ? localization.settings. |
| 84 | : localization.settings. |
| 85 | </AlertDialogTitle> |
| 86 | <AlertDialogDescription> |
| 87 | {isEverywhere |
| 88 | ? localization.settings. |
| 89 | : localization.settings. |
| 90 | </AlertDialogDescription> |
| 91 | </AlertDialogHeader> |
| 92 | <AlertDialogFooter> |
| 93 | <AlertDialogCancel disabled={ |
| 94 | {localization.settings.cancel |
| 95 | </AlertDialogCancel> |
| 96 | <Button |
| 97 | disabled={isPending} |
| 98 | onClick={() => |
| 99 | isEverywhere |
| 100 | ? revokeSessions.mutate() |
| 101 | : revokeOtherSessions.mutate() |
| 102 | } |
| 103 | type="button" |
| 104 | variant={isEverywhere ? |
| 105 | > |
| 106 | {isPending && <Spinner |
| 107 | {isEverywhere |
| 108 | ? localization.settings. |
| 109 | : localization.settings. |
| 110 | </Button> |
| 111 | </AlertDialogFooter> |
| 112 | </AlertDialogContent> |
| 113 | </AlertDialog> |
| 114 | </> |
| 115 | ); |
| 116 | } |