-
Notifications
You must be signed in to change notification settings - Fork 657
/
callback-options.ts
82 lines (74 loc) · 2.98 KB
/
callback-options.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { IncomingMessage, ServerResponse } from 'http';
import { CodedError } from './errors';
import { InstallURLOptions } from './install-url-options';
import { Installation, OrgInstallation } from './installation';
export interface CallbackOptions {
// success is given control after handleCallback() has stored the
// installation. when provided, this function must complete the
// callbackRes.
success?: (
installation: Installation | OrgInstallation,
options: InstallURLOptions,
callbackReq: IncomingMessage,
callbackRes: ServerResponse,
) => void;
// failure is given control when handleCallback() fails at any point.
// when provided, this function must complete the callbackRes.
// default:
// serve a generic "Error" web page (show detailed cause in development)
failure?: (
error: CodedError,
options: InstallURLOptions,
callbackReq: IncomingMessage,
callbackRes: ServerResponse,
) => void;
}
// Default function to call when OAuth flow is successful
export function defaultCallbackSuccess(
installation: Installation,
_options: InstallURLOptions | undefined,
_req: IncomingMessage,
res: ServerResponse,
): void {
let redirectUrl: string;
if (isNotOrgInstall(installation) && installation.appId !== undefined) {
// redirect back to Slack native app
// Changes to the workspace app was installed to, to the app home
redirectUrl = `slack://app?team=${installation.team.id}&id=${installation.appId}`;
} else if (isOrgInstall(installation)) {
// redirect to Slack app management dashboard
redirectUrl = `${installation.enterpriseUrl}manage/organization/apps/profile/${installation.appId}/workspaces/add`;
} else {
// redirect back to Slack native app
// does not change the workspace the slack client was last in
redirectUrl = 'slack://open';
}
const htmlResponse = `<html>
<meta http-equiv="refresh" content="0; URL=${redirectUrl}">
<body>
<h1>Success! Redirecting to the Slack App...</h1>
<button onClick="window.location = '${redirectUrl}'">Click here to redirect</button>
</body></html>`;
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(htmlResponse);
}
// Default function to call when OAuth flow is unsuccessful
export function defaultCallbackFailure(
_error: CodedError,
_options: InstallURLOptions,
_req: IncomingMessage,
res: ServerResponse,
): void {
res.writeHead(500, { 'Content-Type': 'text/html' });
res.end('<html><body><h1>Oops, Something Went Wrong! Please Try Again or Contact the App Owner</h1></body></html>');
}
// ------------------------------------------
// Internals
// ------------------------------------------
// Type guard to narrow Installation type to OrgInstallation
function isOrgInstall(installation: Installation): installation is OrgInstallation {
return installation.isEnterpriseInstall || false;
}
function isNotOrgInstall(installation: Installation): installation is Installation<'v1' | 'v2', false> {
return !(isOrgInstall(installation));
}