Skip to content

Commit a5f79f3

Browse files
Showing account info on successful connetion
1 parent 8b08d2d commit a5f79f3

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

docs-v2/components/AccountConnectionDemo.jsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,22 @@ export default function AccountConnectionDemo() {
6161

6262
{connectedAccount && (
6363
<div className="mt-4 p-3 bg-green-50 border border-green-200 text-green-800 rounded-md">
64-
<div className="font-medium text-sm">Account successfully connected!</div>
65-
<div className="mt-1 text-sm">
66-
<div>Account ID: <span className="font-mono text-xs">{connectedAccount.id}</span></div>
64+
<div className="font-medium text-sm">Successfully connected your {appSlug} account!</div>
65+
<div className="mt-4 text-sm">
66+
{connectedAccount.loading
67+
? (
68+
<div>Loading account details...</div>
69+
)
70+
: (
71+
<>
72+
{connectedAccount.name
73+
? (
74+
<div>Account info: <span className="font-medium">{connectedAccount.name}</span></div>
75+
)
76+
: null}
77+
<div>Account ID: <span className="font-medium">{connectedAccount.id}</span></div>
78+
</>
79+
)}
6780
</div>
6881
</div>
6982
)}

docs-v2/components/GlobalConnectProvider.jsx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,34 @@ export function GlobalConnectProvider({ children }) {
9797
}
9898
}
9999

100+
// Fetch account details from API
101+
async function fetchAccountDetails(accountId) {
102+
try {
103+
// Use the same token credentials to fetch account details
104+
const response = await fetch(`/docs/api-demo-connect/accounts/${accountId}`, {
105+
method: "GET",
106+
headers: {
107+
"Content-Type": "application/json",
108+
},
109+
});
110+
111+
if (!response.ok) {
112+
console.warn("Failed to fetch account details", await response.text());
113+
return {
114+
id: accountId,
115+
}; // Fall back to just the ID
116+
}
117+
118+
const data = await response.json();
119+
return data; // Return the full account details
120+
} catch (err) {
121+
console.warn("Error fetching account details:", err);
122+
return {
123+
id: accountId,
124+
}; // Fall back to just the ID
125+
}
126+
}
127+
100128
// Connect account function
101129
function connectAccount() {
102130
if (!tokenData?.token) {
@@ -111,10 +139,22 @@ export function GlobalConnectProvider({ children }) {
111139
pd.connectAccount({
112140
app: appSlug,
113141
token: tokenData.token,
114-
onSuccess: (account) => {
142+
onSuccess: async (account) => {
143+
// Initialize with just the ID
115144
setConnectedAccount({
116145
id: account.id,
146+
loading: true,
117147
});
148+
149+
// Fetch additional account details
150+
const accountDetails = await fetchAccountDetails(account.id);
151+
152+
// Update with the full details
153+
setConnectedAccount({
154+
...accountDetails,
155+
loading: false,
156+
});
157+
118158
// Token is single-use, so clear it after successful connection
119159
setTokenData(null);
120160
},

docs-v2/next.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,10 @@ export default withNextra({
554554
source: "/api-demo-connect/token",
555555
destination: "/api/demo-connect/token",
556556
},
557+
{
558+
source: "/api-demo-connect/accounts/:id",
559+
destination: "/api/demo-connect/accounts/:id",
560+
},
557561
];
558562
},
559563
env: {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// API route to fetch account details from Pipedream API
2+
import { createBackendClient } from "@pipedream/sdk/server";
3+
4+
export default async function handler(req, res) {
5+
const { id } = req.query;
6+
7+
if (!id) {
8+
return res.status(400).json({
9+
error: "Account ID is required",
10+
});
11+
}
12+
13+
try {
14+
// Initialize the Pipedream SDK client
15+
const pd = createBackendClient({
16+
environment: process.env.PIPEDREAM_PROJECT_ENVIRONMENT || "development",
17+
credentials: {
18+
clientId: process.env.PIPEDREAM_CLIENT_ID,
19+
clientSecret: process.env.PIPEDREAM_CLIENT_SECRET,
20+
},
21+
projectId: process.env.PIPEDREAM_PROJECT_ID,
22+
});
23+
24+
// Fetch the specific account by ID
25+
const accountDetails = await pd.getAccountById(id);
26+
27+
// Return the account details
28+
return res.status(200).json(accountDetails);
29+
} catch (err) {
30+
console.error("Error fetching account details:", err);
31+
return res.status(500).json({
32+
error: "Failed to fetch account details",
33+
message: err.message,
34+
});
35+
}
36+
}

0 commit comments

Comments
 (0)