Skip to content

Commit 1da23f8

Browse files
authored
Merge pull request #3650 from Dokploy/feat/add-linking-accounts-cloud-version
Feat/add linking accounts cloud version
2 parents f5fa39b + b391abf commit 1da23f8

6 files changed

Lines changed: 275 additions & 8 deletions

File tree

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
"use client";
2+
3+
import { Link2, Loader2, Unlink } from "lucide-react";
4+
import { useCallback, useEffect, useState } from "react";
5+
import { toast } from "sonner";
6+
import { Button } from "@/components/ui/button";
7+
import {
8+
Card,
9+
CardContent,
10+
CardDescription,
11+
CardHeader,
12+
CardTitle,
13+
} from "@/components/ui/card";
14+
import { authClient } from "@/lib/auth-client";
15+
16+
const LINKING_CALLBACK_URL = "/dashboard/settings/profile";
17+
18+
const TRUSTED_PROVIDERS = ["google", "github"] as const;
19+
type SocialProvider = (typeof TRUSTED_PROVIDERS)[number];
20+
21+
type AccountItem = {
22+
providerId: string;
23+
accountId?: string;
24+
};
25+
26+
function providerLabel(providerId: string): string {
27+
return providerId.charAt(0).toUpperCase() + providerId.slice(1);
28+
}
29+
30+
export function LinkingAccount() {
31+
const [accounts, setAccounts] = useState<AccountItem[]>([]);
32+
const [accountsLoading, setAccountsLoading] = useState(true);
33+
const [linkingProvider, setLinkingProvider] = useState<SocialProvider | null>(
34+
null,
35+
);
36+
const [unlinkingProviderId, setUnlinkingProviderId] = useState<string | null>(
37+
null,
38+
);
39+
40+
const fetchAccounts = useCallback(async () => {
41+
setAccountsLoading(true);
42+
try {
43+
const { data } = await authClient.listAccounts();
44+
const list = Array.isArray(data)
45+
? data
46+
: ((data && typeof data === "object" && "accounts" in data
47+
? (data as { accounts?: AccountItem[] }).accounts
48+
: null) ?? []);
49+
setAccounts(Array.isArray(list) ? list : []);
50+
} catch {
51+
setAccounts([]);
52+
} finally {
53+
setAccountsLoading(false);
54+
}
55+
}, []);
56+
57+
useEffect(() => {
58+
fetchAccounts();
59+
}, [fetchAccounts]);
60+
61+
const linkedProviderIds = new Set(accounts.map((a) => a.providerId));
62+
const socialAccounts = accounts.filter((a) =>
63+
TRUSTED_PROVIDERS.includes(a.providerId as SocialProvider),
64+
);
65+
66+
const handleLinkSocial = async (provider: SocialProvider) => {
67+
setLinkingProvider(provider);
68+
try {
69+
const { error } = await authClient.linkSocial({
70+
provider,
71+
callbackURL: LINKING_CALLBACK_URL,
72+
});
73+
if (error) {
74+
toast.error(error.message ?? "Failed to link account");
75+
setLinkingProvider(null);
76+
return;
77+
}
78+
} catch (err) {
79+
toast.error(
80+
"Failed to link account",
81+
err instanceof Error ? { description: err.message } : undefined,
82+
);
83+
setLinkingProvider(null);
84+
}
85+
};
86+
87+
const handleUnlink = async (providerId: string, accountId?: string) => {
88+
setUnlinkingProviderId(providerId);
89+
try {
90+
const { error } = await authClient.unlinkAccount({
91+
providerId,
92+
...(accountId && { accountId }),
93+
});
94+
if (error) {
95+
toast.error(error.message ?? "Failed to unlink account");
96+
return;
97+
}
98+
toast.success("Account unlinked");
99+
await fetchAccounts();
100+
} catch (err) {
101+
toast.error(
102+
"Failed to unlink account",
103+
err instanceof Error ? { description: err.message } : undefined,
104+
);
105+
} finally {
106+
setUnlinkingProviderId(null);
107+
}
108+
};
109+
110+
const canUnlink = accounts.length > 1;
111+
112+
return (
113+
<Card className="h-full bg-sidebar p-2.5 rounded-xl max-w-6xl mx-auto w-full">
114+
<div className="rounded-xl bg-background shadow-md">
115+
<CardHeader>
116+
<div className="flex flex-row gap-2 flex-wrap justify-between items-center">
117+
<div>
118+
<CardTitle className="text-xl flex flex-row gap-2">
119+
<Link2 className="size-6 text-muted-foreground self-center" />
120+
Linking account
121+
</CardTitle>
122+
<CardDescription>
123+
Link your Google or GitHub account to sign in with them.
124+
</CardDescription>
125+
</div>
126+
</div>
127+
</CardHeader>
128+
<CardContent className="space-y-6 py-8 border-t">
129+
{/* Linked accounts */}
130+
<div className="space-y-2">
131+
<p className="text-sm font-medium">Linked accounts</p>
132+
{accountsLoading ? (
133+
<div className="flex items-center gap-2 text-sm text-muted-foreground py-2">
134+
<Loader2 className="size-4 animate-spin" />
135+
Loading...
136+
</div>
137+
) : socialAccounts.length === 0 ? (
138+
<p className="text-sm text-muted-foreground py-2">
139+
No social accounts linked yet.
140+
</p>
141+
) : (
142+
<ul className="space-y-2">
143+
{socialAccounts.map((acc) => (
144+
<li
145+
key={acc.accountId ?? acc.providerId}
146+
className="flex items-center justify-between rounded-lg border px-3 py-2 text-sm"
147+
>
148+
<span className="font-medium">
149+
{providerLabel(acc.providerId)}
150+
</span>
151+
{canUnlink && (
152+
<Button
153+
variant="ghost"
154+
size="sm"
155+
className="text-destructive hover:text-destructive hover:bg-destructive/10"
156+
onClick={() =>
157+
handleUnlink(acc.providerId, acc.accountId)
158+
}
159+
disabled={unlinkingProviderId === acc.providerId}
160+
isLoading={unlinkingProviderId === acc.providerId}
161+
>
162+
{unlinkingProviderId === acc.providerId ? (
163+
<Loader2 className="size-4 animate-spin" />
164+
) : (
165+
<>
166+
<Unlink className="mr-1.5 size-4" />
167+
Unlink
168+
</>
169+
)}
170+
</Button>
171+
)}
172+
</li>
173+
))}
174+
</ul>
175+
)}
176+
</div>
177+
178+
<p className="text-sm text-muted-foreground">
179+
Click a provider below to link it to your account. You will be
180+
redirected to complete the flow.
181+
</p>
182+
<div className="flex flex-wrap gap-3">
183+
{!linkedProviderIds.has("google") && (
184+
<Button
185+
variant="outline"
186+
type="button"
187+
className="min-w-[180px]"
188+
onClick={() => handleLinkSocial("google")}
189+
disabled={!!linkingProvider}
190+
isLoading={linkingProvider === "google"}
191+
>
192+
{linkingProvider === "google" ? (
193+
<Loader2 className="mr-2 size-4 animate-spin" />
194+
) : (
195+
<svg viewBox="0 0 24 24" className="mr-2 size-4">
196+
<path
197+
fill="currentColor"
198+
d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
199+
/>
200+
<path
201+
fill="currentColor"
202+
d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
203+
/>
204+
<path
205+
fill="currentColor"
206+
d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
207+
/>
208+
<path
209+
fill="currentColor"
210+
d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
211+
/>
212+
</svg>
213+
)}
214+
Link with Google
215+
</Button>
216+
)}
217+
{!linkedProviderIds.has("github") && (
218+
<Button
219+
variant="outline"
220+
type="button"
221+
className="min-w-[180px]"
222+
onClick={() => handleLinkSocial("github")}
223+
disabled={!!linkingProvider}
224+
isLoading={linkingProvider === "github"}
225+
>
226+
{linkingProvider === "github" ? (
227+
<Loader2 className="mr-2 size-4 animate-spin" />
228+
) : (
229+
<svg
230+
viewBox="0 0 24 24"
231+
className="mr-2 size-4"
232+
fill="currentColor"
233+
>
234+
<path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z" />
235+
</svg>
236+
)}
237+
Link with GitHub
238+
</Button>
239+
)}
240+
</div>
241+
</CardContent>
242+
</div>
243+
</Card>
244+
);
245+
}

apps/dokploy/pages/dashboard/settings/profile.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { GetServerSidePropsContext } from "next";
44
import type { ReactElement } from "react";
55
import superjson from "superjson";
66
import { ShowApiKeys } from "@/components/dashboard/settings/api/show-api-keys";
7+
import { LinkingAccount } from "@/components/dashboard/settings/linking-account/linking-account";
78
import { ProfileForm } from "@/components/dashboard/settings/profile/profile-form";
89
import { DashboardLayout } from "@/components/layouts/dashboard-layout";
910
import { appRouter } from "@/server/api/root";
@@ -12,17 +13,16 @@ import { getLocale, serverSideTranslations } from "@/utils/i18n";
1213

1314
const Page = () => {
1415
const { data } = api.user.get.useQuery();
16+
const { data: isCloud } = api.settings.isCloud.useQuery();
1517

16-
// const { data: isCloud } = api.settings.isCloud.useQuery();
1718
return (
1819
<div className="w-full">
19-
<div className="h-full rounded-xl max-w-5xl mx-auto flex flex-col gap-4">
20+
<div className="h-full rounded-xl max-w-5xl mx-auto flex flex-col gap-4">
2021
<ProfileForm />
22+
{isCloud && <LinkingAccount />}
2123
{(data?.canAccessToAPI ||
2224
data?.role === "owner" ||
2325
data?.role === "admin") && <ShowApiKeys />}
24-
25-
{/* {isCloud && <RemoveSelfAccount />} */}
2626
</div>
2727
</div>
2828
);

apps/dokploy/server/api/routers/stripe.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {
77
import { TRPCError } from "@trpc/server";
88
import Stripe from "stripe";
99
import { z } from "zod";
10-
import { getStripeItems, WEBSITE_URL } from "@/server/utils/stripe";
10+
import {
11+
getStripeItems,
12+
PRODUCT_ANNUAL_ID,
13+
PRODUCT_MONTHLY_ID,
14+
WEBSITE_URL,
15+
} from "@/server/utils/stripe";
1116
import { adminProcedure, createTRPCRouter } from "../trpc";
1217

1318
export const stripeRouter = createTRPCRouter({
@@ -22,6 +27,7 @@ export const stripeRouter = createTRPCRouter({
2227
const products = await stripe.products.list({
2328
expand: ["data.default_price"],
2429
active: true,
30+
ids: [PRODUCT_MONTHLY_ID, PRODUCT_ANNUAL_ID],
2531
});
2632

2733
if (!stripeCustomerId) {

apps/dokploy/server/utils/enterprise.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ function isNetworkError(error: unknown): boolean {
88
if (error.message === "fetch failed") return true;
99
const cause = (error as Error & { cause?: { code?: string } }).cause;
1010
const code = cause?.code;
11-
return code === "ECONNREFUSED" || code === "ENOTFOUND" || code === "ETIMEDOUT";
11+
return (
12+
code === "ECONNREFUSED" || code === "ENOTFOUND" || code === "ETIMEDOUT"
13+
);
1214
}
1315
return false;
1416
}

apps/dokploy/server/utils/stripe.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ export const WEBSITE_URL =
33
? "http://localhost:3000"
44
: process.env.SITE_URL;
55

6-
const BASE_PRICE_MONTHLY_ID = process.env.BASE_PRICE_MONTHLY_ID!; // $4.00
6+
export const BASE_PRICE_MONTHLY_ID = process.env.BASE_PRICE_MONTHLY_ID!; // $4.00
77

8-
const BASE_ANNUAL_MONTHLY_ID = process.env.BASE_ANNUAL_MONTHLY_ID!; // $7.99
8+
export const BASE_ANNUAL_MONTHLY_ID = process.env.BASE_ANNUAL_MONTHLY_ID!; // $7.99
9+
10+
export const PRODUCT_MONTHLY_ID = process.env.PRODUCT_MONTHLY_ID!;
11+
export const PRODUCT_ANNUAL_ID = process.env.PRODUCT_ANNUAL_ID!;
912

1013
export const getStripeItems = (serverQuantity: number, isAnnual: boolean) => {
1114
const items = [];

packages/server/src/lib/auth.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ const { handler, api } = betterAuth({
4343
},
4444
}
4545
: {}),
46+
...(IS_CLOUD
47+
? {
48+
account: {
49+
accountLinking: {
50+
enabled: true,
51+
trustedProviders: ["github", "google"],
52+
allowDifferentEmails: true,
53+
},
54+
},
55+
}
56+
: {}),
4657
appName: "Dokploy",
4758
socialProviders: {
4859
github: {

0 commit comments

Comments
 (0)