From 6981e0098a62e6a0d2b068fe150cb00bb5d15e9b Mon Sep 17 00:00:00 2001
From: Roland <33993199+rolznz@users.noreply.github.com>
Date: Fri, 29 Nov 2024 14:54:15 -0600
Subject: [PATCH] Feat: disconnect alby account in auth code form (#833)
* feat: disconnect from Alby Account in OAuth form
* fix: always show confirmation dialog when disconnecting alby account
---
alby/alby_oauth_service.go | 5 +-
frontend/src/components/AuthCodeForm.tsx | 13 +-
frontend/src/components/UnlinkAlbyAccount.tsx | 78 +++++++++
frontend/src/screens/settings/AlbyAccount.tsx | 153 ++++--------------
4 files changed, 120 insertions(+), 129 deletions(-)
create mode 100644 frontend/src/components/UnlinkAlbyAccount.tsx
diff --git a/alby/alby_oauth_service.go b/alby/alby_oauth_service.go
index 06e0985f..35da0ef8 100644
--- a/alby/alby_oauth_service.go
+++ b/alby/alby_oauth_service.go
@@ -554,8 +554,9 @@ func (svc *albyOAuthService) UnlinkAccount(ctx context.Context) error {
return errors.New("alby account cannot be unlinked while VSS is activated")
}
- err = svc.destroyAlbyAccountNWCNode(ctx)
- if err != nil {
+ destroyAlbyAccountErr := svc.destroyAlbyAccountNWCNode(ctx)
+ if destroyAlbyAccountErr != nil {
+ // non-critical error - we still want to disconnect
logger.Logger.WithError(err).Error("Failed to destroy Alby Account NWC node")
}
svc.deleteAlbyAccountApps()
diff --git a/frontend/src/components/AuthCodeForm.tsx b/frontend/src/components/AuthCodeForm.tsx
index 2f039bb9..472b4a6f 100644
--- a/frontend/src/components/AuthCodeForm.tsx
+++ b/frontend/src/components/AuthCodeForm.tsx
@@ -8,6 +8,7 @@ import { Input } from "src/components/ui/input";
import { Label } from "src/components/ui/label";
import { LoadingButton } from "src/components/ui/loading-button";
import { useToast } from "src/components/ui/use-toast";
+import { UnlinkAlbyAccount } from "src/components/UnlinkAlbyAccount";
import { useInfo } from "src/hooks/useInfo";
import { handleRequestError } from "src/utils/handleRequestError";
@@ -23,7 +24,7 @@ function AuthCodeForm({ url }: AuthCodeFormProps) {
const navigate = useNavigate();
const { toast } = useToast();
- const { mutate: refetchInfo } = useInfo();
+ const { data: info, mutate: refetchInfo } = useInfo();
const [hasRequestedCode, setRequestedCode] = React.useState(false);
const [isLoading, setLoading] = React.useState(false);
@@ -103,6 +104,16 @@ function AuthCodeForm({ url }: AuthCodeFormProps) {
>
)}
+ {info?.albyUserIdentifier && (
+
+
or
+
+
+
+
+ )}
diff --git a/frontend/src/components/UnlinkAlbyAccount.tsx b/frontend/src/components/UnlinkAlbyAccount.tsx
new file mode 100644
index 00000000..fa07023f
--- /dev/null
+++ b/frontend/src/components/UnlinkAlbyAccount.tsx
@@ -0,0 +1,78 @@
+import React from "react";
+import { useNavigate } from "react-router-dom";
+import {
+ AlertDialog,
+ AlertDialogAction,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+ AlertDialogTrigger,
+} from "src/components/ui/alert-dialog";
+import { useToast } from "src/components/ui/use-toast";
+import { useInfo } from "src/hooks/useInfo";
+import { request } from "src/utils/request";
+
+type UnlinkAlbyAccountProps = {
+ navigateTo?: string;
+ successMessage?: string;
+};
+
+export function UnlinkAlbyAccount({
+ children,
+ navigateTo = "/",
+ successMessage = "Your hub is no longer connected to an Alby Account.",
+}: React.PropsWithChildren) {
+ const { toast } = useToast();
+ const navigate = useNavigate();
+ const { mutate: refetchInfo } = useInfo();
+
+ const unlinkAccount = React.useCallback(async () => {
+ try {
+ await request("/api/alby/unlink-account", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ });
+ await refetchInfo();
+ navigate(navigateTo);
+ toast({
+ title: "Alby Account Disconnected",
+ description: successMessage,
+ });
+ } catch (error) {
+ toast({
+ title: "Disconnect account failed",
+ description: (error as Error).message,
+ variant: "destructive",
+ });
+ }
+ }, [refetchInfo, navigate, navigateTo, toast, successMessage]);
+
+ return (
+
+ {children}
+
+
+ Disconnect Alby Account
+
+
+
Are you sure you want to disconnect your Alby Account?
+
+ Your Alby Account will be disconnected and all Alby Account
+ features such as your lightning address will stop working.
+
+
+
+
+
+ Cancel
+ Confirm
+
+
+
+ );
+}
diff --git a/frontend/src/screens/settings/AlbyAccount.tsx b/frontend/src/screens/settings/AlbyAccount.tsx
index b3f42961..cb83c592 100644
--- a/frontend/src/screens/settings/AlbyAccount.tsx
+++ b/frontend/src/screens/settings/AlbyAccount.tsx
@@ -1,77 +1,17 @@
import { ExitIcon } from "@radix-ui/react-icons";
import { ExternalLinkIcon } from "lucide-react";
-import { useNavigate } from "react-router-dom";
import ExternalLink from "src/components/ExternalLink";
import SettingsHeader from "src/components/SettingsHeader";
-import {
- AlertDialog,
- AlertDialogAction,
- AlertDialogCancel,
- AlertDialogContent,
- AlertDialogDescription,
- AlertDialogFooter,
- AlertDialogHeader,
- AlertDialogTitle,
- AlertDialogTrigger,
-} from "src/components/ui/alert-dialog";
import {
Card,
CardDescription,
CardHeader,
CardTitle,
} from "src/components/ui/card";
-import { useToast } from "src/components/ui/use-toast";
-
-import { request } from "src/utils/request";
+import { UnlinkAlbyAccount } from "src/components/UnlinkAlbyAccount";
export function AlbyAccount() {
- const { toast } = useToast();
- const navigate = useNavigate();
-
- const disconnect = async () => {
- try {
- await request("/api/alby/unlink-account", {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- });
- navigate("/");
- toast({
- title: "Alby Account Disconnected",
- description: "Your hub is no longer connected to an Alby Account.",
- });
- } catch (error) {
- toast({
- title: "Disconnect account failed",
- description: (error as Error).message,
- variant: "destructive",
- });
- }
- };
- const unlink = async () => {
- try {
- await request("/api/alby/unlink-account", {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- });
- navigate("/alby/auth?force_login=true");
- toast({
- title: "Alby Account Unlinked",
- description: "Please login with another Alby Account",
- });
- } catch (error) {
- toast({
- title: "Unlink account failed",
- description: (error as Error).message,
- variant: "destructive",
- });
- }
- };
-
return (
<>
-
-
-
-
- Change Alby Account
-
- Link your Hub to a different
- Alby Account
-
-
-
-
-
-
- Unlink Alby Account
-
-
-
- Are you sure you want to change the Alby Account for your hub?
-
-
- Your Alby Account will be disconnected from your hub and
- you'll need to login with a new Alby Account to access your
- hub.
-
-
-
-
-
- Cancel
- Confirm
-
-
-
-
-
-
-
- Disconnect Alby Account
-
- Use Alby Hub without an Alby
- Account
-
-
-
-
-
-
- Disconnect Alby Account
-
-
-
Are you sure you want to disconnect your Alby Account?
-
- Your Alby Account will be disconnected and all Alby Account
- features such as your lightning address will stop working.
-
-
-
-
-
- Cancel
- Confirm
-
-
-
+
+
+
+ Change Alby Account
+
+ Link your Hub to a different Alby
+ Account
+
+
+
+
+
+
+
+
+ Disconnect Alby Account
+
+ Use Alby Hub without an Alby
+ Account
+
+
+
+
>
);
}