-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathauthProvisions.ts
46 lines (37 loc) · 1.37 KB
/
authProvisions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { createPdClient } from "./pd-client.js"
import { config } from "./config.js"
import { Account } from "@pipedream/sdk"
export const getAuthProvision = async ({
app,
externalUserId,
}: {
app: string
externalUserId: string
}): Promise<Account | string> => {
const pd = createPdClient()
// Get existing auth provisions for this user and app
const authProvisions = await pd.getAccounts({
external_user_id: externalUserId,
include_credentials: false,
app,
})
// "healthy" auth provisions have active, valid OAuth grants / credentials
// This naive implementation returns the first healthy auth provision for the app
const authProvision = authProvisions.data.find((ap) => ap.healthy)
// If no healthy auth provision exists, return a Pipedream-hosted link
// your users can use to connect their account.
// https://pipedream.com/docs/connect/managed-auth/connect-link/
if (!authProvision) {
const token = await pd.createConnectToken({
external_user_id: externalUserId,
webhook_uri: config.pipedream.webhookUri,
})
return `
You need to connect your account to call this tool.
Please visit this URL to securely authenticate with ${app}:
https://pipedream.com/_static/connect.html?token=${token.token}&connectLink=true&app=${encodeURIComponent(app)}
Let me know when you're done.
`.trim()
}
return authProvision
}