Skip to content

Commit e930db3

Browse files
author
Fil Maj
authored
chore: updates to example apps (#2007)
1 parent d9c9a66 commit e930db3

File tree

4 files changed

+78
-78
lines changed

4 files changed

+78
-78
lines changed

examples/legacy-sign-in-with-slack/app.js

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
// https://api.slack.com/legacy/sign-in-with-slack
2-
const Koa = require("koa");
3-
const Router = require("@koa/router");
4-
const { WebClient } = require("@slack/web-api");
5-
const uuid = require("uuid");
2+
const Koa = require('koa');
3+
const Router = require('@koa/router');
4+
const { WebClient } = require('@slack/web-api');
5+
const uuid = require('uuid');
66

77
const app = new Koa();
88
const router = new Router();
99

10-
router.get("/", async (ctx) => {
11-
ctx.redirect("/slack/install");
10+
router.get('/', async (ctx) => {
11+
ctx.redirect('/slack/install');
1212
});
1313

1414
const clientId = process.env.SLACK_CLIENT_ID;
1515
const clientSecret = process.env.SLACK_CLIENT_SECRET;
16-
const legacyIdentityScopes = "identity.basic,identity.email"; // identity.basic is required at least
16+
const legacyIdentityScopes = 'identity.basic,identity.email'; // identity.basic is required at least
1717

1818
class MyStateStore {
1919
constructor() {
@@ -35,11 +35,11 @@ class MyStateStore {
3535
}
3636
const myStateStore = new MyStateStore();
3737

38-
router.get("/slack/install", async (ctx) => {
38+
router.get('/slack/install', async (ctx) => {
3939
const state = await myStateStore.generate();
4040
const url = `https://slack.com/oauth/v2/authorize?state=${state}&client_id=${clientId}&user_scope=${legacyIdentityScopes}`;
4141

42-
ctx.headers["content-type"] = "text/html; charset=utf-8";
42+
ctx.headers['content-type'] = 'text/html; charset=utf-8';
4343
ctx.body = `<html>
4444
<head><style>body {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
4545
<body>
@@ -51,10 +51,10 @@ router.get("/slack/install", async (ctx) => {
5151

5252
const client = new WebClient();
5353

54-
router.get("/slack/oauth_redirect", async (ctx) => {
54+
router.get('/slack/oauth_redirect', async (ctx) => {
5555
if (!(await myStateStore.validate(ctx.query.state))) {
5656
ctx.status = 400;
57-
ctx.headers["content-type"] = "text/html; charset=utf-8";
57+
ctx.headers['content-type'] = 'text/html; charset=utf-8';
5858
ctx.body = `<html>
5959
<head><style>body {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
6060
<body>
@@ -70,9 +70,7 @@ router.get("/slack/oauth_redirect", async (ctx) => {
7070
client_secret: clientSecret,
7171
code: ctx.query.code,
7272
});
73-
console.log(
74-
`oauth.v2.access response: ${JSON.stringify(token, null, 2)}`
75-
);
73+
console.log(`oauth.v2.access response: ${JSON.stringify(token, null, 2)}`);
7674

7775
let userAccessToken = token.authed_user.access_token;
7876

@@ -83,16 +81,10 @@ router.get("/slack/oauth_redirect", async (ctx) => {
8381
const refreshedToken = await client.oauth.v2.access({
8482
client_id: clientId,
8583
client_secret: clientSecret,
86-
grant_type: "refresh_token",
84+
grant_type: 'refresh_token',
8785
refresh_token: token.refresh_token,
8886
});
89-
console.log(
90-
`oauth.v2.access (refresh) response: ${JSON.stringify(
91-
refreshedToken,
92-
null,
93-
2
94-
)}`
95-
);
87+
console.log(`oauth.v2.access (refresh) response: ${JSON.stringify(refreshedToken, null, 2)}`);
9688
userAccessToken = refreshedToken.access_token;
9789
}
9890

@@ -103,11 +95,9 @@ router.get("/slack/oauth_redirect", async (ctx) => {
10395
// You don't need to do this here.
10496
const tokenWiredClient = new WebClient(userAccessToken);
10597
const userInfo = await tokenWiredClient.users.identity();
106-
console.log(
107-
`users.identity response: ${JSON.stringify(userInfo, null, 2)}`
108-
);
98+
console.log(`users.identity response: ${JSON.stringify(userInfo, null, 2)}`);
10999

110-
ctx.headers["content-type"] = "text/html; charset=utf-8";
100+
ctx.headers['content-type'] = 'text/html; charset=utf-8';
111101
ctx.body = `<html>
112102
<head><style>body h2 {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
113103
<body>
@@ -120,4 +110,6 @@ router.get("/slack/oauth_redirect", async (ctx) => {
120110
// Enable the routes
121111
app.use(router.routes()).use(router.allowedMethods());
122112
// Start the web app, which is available at http://localhost:3000/slack/*
123-
app.listen(3000);
113+
app.listen(3000, () => {
114+
console.log('app started');
115+
});

examples/legacy-sign-in-with-slack/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"repository": "slackapi/node-slack-sdk",
1212
"dependencies": {
1313
"@koa/router": "^13.0.0",
14+
"@slack/bolt": "^3.21.2",
1415
"koa": "^2.13.1",
1516
"uuid": "^10.0.0"
1617
}

examples/oauth-v2/app.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ const slackEvents = createEventAdapter(process.env.SLACK_SIGNING_SECRET, {
1313
// Set path to receive events
1414
app.use('/slack/events', slackEvents.requestListener());
1515

16-
const scopes = ['app_mentions:read', 'channels:read', 'groups:read', 'channels:manage', 'chat:write', 'incoming-webhook'];
16+
const scopes = [
17+
'app_mentions:read',
18+
'channels:read',
19+
'groups:read',
20+
'channels:manage',
21+
'chat:write',
22+
'incoming-webhook',
23+
];
1724
const userScopes = ['chat:write'];
1825

1926
const installer = new InstallProvider({
@@ -27,14 +34,19 @@ const installer = new InstallProvider({
2734
logLevel: LogLevel.DEBUG,
2835
});
2936

30-
app.get('/', (req, res) => res.send('go to /slack/install'));
37+
app.get('/', (_req, res) => res.send('go to /slack/install'));
3138

32-
app.get('/slack/install', async (req, res, next) => {
33-
await installer.handleInstallPath(req, res, {}, {
34-
scopes,
35-
userScopes,
36-
metadata: 'some_metadata',
37-
});
39+
app.get('/slack/install', async (req, res, _next) => {
40+
await installer.handleInstallPath(
41+
req,
42+
res,
43+
{},
44+
{
45+
scopes,
46+
userScopes,
47+
metadata: 'some_metadata',
48+
},
49+
);
3850
});
3951

4052
// This works since @slack/[email protected] or newer
@@ -120,24 +132,27 @@ slackEvents.on('app_home_opened', async (event, body) => {
120132
const web = new WebClient(DBInstallData.botToken);
121133
await web.views.publish({
122134
user_id: event.user,
123-
view: {
124-
"type":"home",
125-
"blocks":[
135+
view: {
136+
type: 'home',
137+
blocks: [
126138
{
127-
"type": "section",
128-
"block_id": "section678",
129-
"text": {
130-
"type": "mrkdwn",
131-
"text": "Welcome to the App Home!"
139+
type: 'section',
140+
block_id: 'section678',
141+
text: {
142+
type: 'mrkdwn',
143+
text: 'Welcome to the App Home!',
132144
},
133-
}
134-
]
145+
},
146+
],
135147
},
136148
});
137149
}
138-
}
139-
catch (error) {
150+
} catch (error) {
140151
console.error(error);
141152
}
142153
});
143-
app.listen(port, () => console.log(`Example app listening on port ${port}! Go to http://localhost:3000/slack/install to initiate oauth flow`))
154+
app.listen(port, () =>
155+
console.log(
156+
`Example app listening on port ${port}! Go to http://localhost:3000/slack/install to initiate oauth flow`,
157+
),
158+
);

examples/openid-connect/app.js

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
const Koa = require("koa");
2-
const Router = require("@koa/router");
3-
const { WebClient } = require("@slack/web-api"); // requires v6.4 or higher
4-
const jwt = require("jsonwebtoken");
5-
const uuid = require("uuid");
1+
const Koa = require('koa');
2+
const Router = require('@koa/router');
3+
const { WebClient } = require('@slack/web-api'); // requires v6.4 or higher
4+
const jwt = require('jsonwebtoken');
5+
const uuid = require('uuid');
66

77
const app = new Koa();
88
const router = new Router();
99

10-
router.get("/", async (ctx) => {
11-
ctx.redirect("/slack/install");
10+
router.get('/', async (ctx) => {
11+
ctx.redirect('/slack/install');
1212
});
1313

1414
const clientId = process.env.SLACK_CLIENT_ID;
1515
const clientSecret = process.env.SLACK_CLIENT_SECRET;
16-
const oidcScopes = "openid,email,profile"; // openid is required at least
16+
const oidcScopes = 'openid,email,profile'; // openid is required at least
1717
const redirectUri = process.env.SLACK_REDIRECT_URI;
1818

1919
class MyStateStore {
@@ -36,14 +36,14 @@ class MyStateStore {
3636
}
3737
const myStateStore = new MyStateStore();
3838

39-
router.get("/slack/install", async (ctx) => {
39+
router.get('/slack/install', async (ctx) => {
4040
const state = await myStateStore.generate();
4141
// (optional) you can pass nonce parameter as well
4242
// refer to https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest for details
43-
const nonce = "your-own-nonce-value";
43+
const nonce = 'your-own-nonce-value';
4444
const url = `https://slack.com/openid/connect/authorize?response_type=code&state=${state}&client_id=${clientId}&scope=${oidcScopes}&redirect_uri=${redirectUri}&nonce=${nonce}`;
4545

46-
ctx.headers["content-type"] = "text/html; charset=utf-8";
46+
ctx.headers['content-type'] = 'text/html; charset=utf-8';
4747
ctx.body = `<html>
4848
<head><style>body {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
4949
<body>
@@ -55,10 +55,10 @@ router.get("/slack/install", async (ctx) => {
5555

5656
const client = new WebClient();
5757

58-
router.get("/slack/oauth_redirect", async (ctx) => {
58+
router.get('/slack/oauth_redirect', async (ctx) => {
5959
if (!(await myStateStore.validate(ctx.query.state))) {
6060
ctx.status = 400;
61-
ctx.headers["content-type"] = "text/html; charset=utf-8";
61+
ctx.headers['content-type'] = 'text/html; charset=utf-8';
6262
ctx.body = `<html>
6363
<head><style>body {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
6464
<body>
@@ -72,12 +72,10 @@ router.get("/slack/oauth_redirect", async (ctx) => {
7272
const token = await client.openid.connect.token({
7373
client_id: clientId,
7474
client_secret: clientSecret,
75-
grant_type: "authorization_code",
75+
grant_type: 'authorization_code',
7676
code: ctx.query.code,
7777
});
78-
console.log(
79-
`openid.connect.token response: ${JSON.stringify(token, null, 2)}`
80-
);
78+
console.log(`openid.connect.token response: ${JSON.stringify(token, null, 2)}`);
8179

8280
let userAccessToken = token.access_token;
8381

@@ -88,16 +86,10 @@ router.get("/slack/oauth_redirect", async (ctx) => {
8886
const refreshedToken = await client.openid.connect.token({
8987
client_id: clientId,
9088
client_secret: clientSecret,
91-
grant_type: "refresh_token",
89+
grant_type: 'refresh_token',
9290
refresh_token: token.refresh_token,
9391
});
94-
console.log(
95-
`openid.connect.token (refresh) response: ${JSON.stringify(
96-
refreshedToken,
97-
null,
98-
2
99-
)}`
100-
);
92+
console.log(`openid.connect.token (refresh) response: ${JSON.stringify(refreshedToken, null, 2)}`);
10193
userAccessToken = refreshedToken.access_token;
10294
}
10395

@@ -111,11 +103,9 @@ router.get("/slack/oauth_redirect", async (ctx) => {
111103
// You don't need to do this here.
112104
const tokenWiredClient = new WebClient(userAccessToken);
113105
const userInfo = await tokenWiredClient.openid.connect.userInfo();
114-
console.log(
115-
`openid.connect.userInfo response: ${JSON.stringify(userInfo, null, 2)}`
116-
);
106+
console.log(`openid.connect.userInfo response: ${JSON.stringify(userInfo, null, 2)}`);
117107

118-
ctx.headers["content-type"] = "text/html; charset=utf-8";
108+
ctx.headers['content-type'] = 'text/html; charset=utf-8';
119109
ctx.body = `<html>
120110
<head><style>body h2 {padding: 10px 15px;font-family: verdana;text-align: center;}</style></head>
121111
<body>
@@ -130,4 +120,6 @@ router.get("/slack/oauth_redirect", async (ctx) => {
130120
// Enable the routes
131121
app.use(router.routes()).use(router.allowedMethods());
132122
// Start the web app, which is available at http://localhost:3000/slack/*
133-
app.listen(3000);
123+
app.listen(3000, () => {
124+
console.log('app started');
125+
});

0 commit comments

Comments
 (0)