diff --git a/examples/legacy-sign-in-with-slack/app.js b/examples/legacy-sign-in-with-slack/app.js index 511fd2340..56526392d 100644 --- a/examples/legacy-sign-in-with-slack/app.js +++ b/examples/legacy-sign-in-with-slack/app.js @@ -1,19 +1,19 @@ // https://api.slack.com/legacy/sign-in-with-slack -const Koa = require("koa"); -const Router = require("@koa/router"); -const { WebClient } = require("@slack/web-api"); -const uuid = require("uuid"); +const Koa = require('koa'); +const Router = require('@koa/router'); +const { WebClient } = require('@slack/web-api'); +const uuid = require('uuid'); const app = new Koa(); const router = new Router(); -router.get("/", async (ctx) => { - ctx.redirect("/slack/install"); +router.get('/', async (ctx) => { + ctx.redirect('/slack/install'); }); const clientId = process.env.SLACK_CLIENT_ID; const clientSecret = process.env.SLACK_CLIENT_SECRET; -const legacyIdentityScopes = "identity.basic,identity.email"; // identity.basic is required at least +const legacyIdentityScopes = 'identity.basic,identity.email'; // identity.basic is required at least class MyStateStore { constructor() { @@ -35,11 +35,11 @@ class MyStateStore { } const myStateStore = new MyStateStore(); -router.get("/slack/install", async (ctx) => { +router.get('/slack/install', async (ctx) => { const state = await myStateStore.generate(); const url = `https://slack.com/oauth/v2/authorize?state=${state}&client_id=${clientId}&user_scope=${legacyIdentityScopes}`; - ctx.headers["content-type"] = "text/html; charset=utf-8"; + ctx.headers['content-type'] = 'text/html; charset=utf-8'; ctx.body = `
@@ -51,10 +51,10 @@ router.get("/slack/install", async (ctx) => { const client = new WebClient(); -router.get("/slack/oauth_redirect", async (ctx) => { +router.get('/slack/oauth_redirect', async (ctx) => { if (!(await myStateStore.validate(ctx.query.state))) { ctx.status = 400; - ctx.headers["content-type"] = "text/html; charset=utf-8"; + ctx.headers['content-type'] = 'text/html; charset=utf-8'; ctx.body = ` @@ -70,9 +70,7 @@ router.get("/slack/oauth_redirect", async (ctx) => { client_secret: clientSecret, code: ctx.query.code, }); - console.log( - `oauth.v2.access response: ${JSON.stringify(token, null, 2)}` - ); + console.log(`oauth.v2.access response: ${JSON.stringify(token, null, 2)}`); let userAccessToken = token.authed_user.access_token; @@ -83,16 +81,10 @@ router.get("/slack/oauth_redirect", async (ctx) => { const refreshedToken = await client.oauth.v2.access({ client_id: clientId, client_secret: clientSecret, - grant_type: "refresh_token", + grant_type: 'refresh_token', refresh_token: token.refresh_token, }); - console.log( - `oauth.v2.access (refresh) response: ${JSON.stringify( - refreshedToken, - null, - 2 - )}` - ); + console.log(`oauth.v2.access (refresh) response: ${JSON.stringify(refreshedToken, null, 2)}`); userAccessToken = refreshedToken.access_token; } @@ -103,11 +95,9 @@ router.get("/slack/oauth_redirect", async (ctx) => { // You don't need to do this here. const tokenWiredClient = new WebClient(userAccessToken); const userInfo = await tokenWiredClient.users.identity(); - console.log( - `users.identity response: ${JSON.stringify(userInfo, null, 2)}` - ); + console.log(`users.identity response: ${JSON.stringify(userInfo, null, 2)}`); - ctx.headers["content-type"] = "text/html; charset=utf-8"; + ctx.headers['content-type'] = 'text/html; charset=utf-8'; ctx.body = ` @@ -120,4 +110,6 @@ router.get("/slack/oauth_redirect", async (ctx) => { // Enable the routes app.use(router.routes()).use(router.allowedMethods()); // Start the web app, which is available at http://localhost:3000/slack/* -app.listen(3000); +app.listen(3000, () => { + console.log('app started'); +}); diff --git a/examples/legacy-sign-in-with-slack/package.json b/examples/legacy-sign-in-with-slack/package.json index d6c4c742b..11d5b8f8c 100644 --- a/examples/legacy-sign-in-with-slack/package.json +++ b/examples/legacy-sign-in-with-slack/package.json @@ -11,6 +11,7 @@ "repository": "slackapi/node-slack-sdk", "dependencies": { "@koa/router": "^13.0.0", + "@slack/bolt": "^3.21.2", "koa": "^2.13.1", "uuid": "^10.0.0" } diff --git a/examples/oauth-v2/app.js b/examples/oauth-v2/app.js index b9644b1f8..0c63d42b2 100644 --- a/examples/oauth-v2/app.js +++ b/examples/oauth-v2/app.js @@ -13,7 +13,14 @@ const slackEvents = createEventAdapter(process.env.SLACK_SIGNING_SECRET, { // Set path to receive events app.use('/slack/events', slackEvents.requestListener()); -const scopes = ['app_mentions:read', 'channels:read', 'groups:read', 'channels:manage', 'chat:write', 'incoming-webhook']; +const scopes = [ + 'app_mentions:read', + 'channels:read', + 'groups:read', + 'channels:manage', + 'chat:write', + 'incoming-webhook', +]; const userScopes = ['chat:write']; const installer = new InstallProvider({ @@ -27,14 +34,19 @@ const installer = new InstallProvider({ logLevel: LogLevel.DEBUG, }); -app.get('/', (req, res) => res.send('go to /slack/install')); +app.get('/', (_req, res) => res.send('go to /slack/install')); -app.get('/slack/install', async (req, res, next) => { - await installer.handleInstallPath(req, res, {}, { - scopes, - userScopes, - metadata: 'some_metadata', - }); +app.get('/slack/install', async (req, res, _next) => { + await installer.handleInstallPath( + req, + res, + {}, + { + scopes, + userScopes, + metadata: 'some_metadata', + }, + ); }); // This works since @slack/oauth@2.5.0 or newer @@ -120,24 +132,27 @@ slackEvents.on('app_home_opened', async (event, body) => { const web = new WebClient(DBInstallData.botToken); await web.views.publish({ user_id: event.user, - view: { - "type":"home", - "blocks":[ + view: { + type: 'home', + blocks: [ { - "type": "section", - "block_id": "section678", - "text": { - "type": "mrkdwn", - "text": "Welcome to the App Home!" + type: 'section', + block_id: 'section678', + text: { + type: 'mrkdwn', + text: 'Welcome to the App Home!', }, - } - ] + }, + ], }, }); } - } - catch (error) { + } catch (error) { console.error(error); } }); -app.listen(port, () => console.log(`Example app listening on port ${port}! Go to http://localhost:3000/slack/install to initiate oauth flow`)) +app.listen(port, () => + console.log( + `Example app listening on port ${port}! Go to http://localhost:3000/slack/install to initiate oauth flow`, + ), +); diff --git a/examples/openid-connect/app.js b/examples/openid-connect/app.js index dbd20968b..950ba674b 100644 --- a/examples/openid-connect/app.js +++ b/examples/openid-connect/app.js @@ -1,19 +1,19 @@ -const Koa = require("koa"); -const Router = require("@koa/router"); -const { WebClient } = require("@slack/web-api"); // requires v6.4 or higher -const jwt = require("jsonwebtoken"); -const uuid = require("uuid"); +const Koa = require('koa'); +const Router = require('@koa/router'); +const { WebClient } = require('@slack/web-api'); // requires v6.4 or higher +const jwt = require('jsonwebtoken'); +const uuid = require('uuid'); const app = new Koa(); const router = new Router(); -router.get("/", async (ctx) => { - ctx.redirect("/slack/install"); +router.get('/', async (ctx) => { + ctx.redirect('/slack/install'); }); const clientId = process.env.SLACK_CLIENT_ID; const clientSecret = process.env.SLACK_CLIENT_SECRET; -const oidcScopes = "openid,email,profile"; // openid is required at least +const oidcScopes = 'openid,email,profile'; // openid is required at least const redirectUri = process.env.SLACK_REDIRECT_URI; class MyStateStore { @@ -36,14 +36,14 @@ class MyStateStore { } const myStateStore = new MyStateStore(); -router.get("/slack/install", async (ctx) => { +router.get('/slack/install', async (ctx) => { const state = await myStateStore.generate(); // (optional) you can pass nonce parameter as well // refer to https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest for details - const nonce = "your-own-nonce-value"; + const nonce = 'your-own-nonce-value'; const url = `https://slack.com/openid/connect/authorize?response_type=code&state=${state}&client_id=${clientId}&scope=${oidcScopes}&redirect_uri=${redirectUri}&nonce=${nonce}`; - ctx.headers["content-type"] = "text/html; charset=utf-8"; + ctx.headers['content-type'] = 'text/html; charset=utf-8'; ctx.body = ` @@ -55,10 +55,10 @@ router.get("/slack/install", async (ctx) => { const client = new WebClient(); -router.get("/slack/oauth_redirect", async (ctx) => { +router.get('/slack/oauth_redirect', async (ctx) => { if (!(await myStateStore.validate(ctx.query.state))) { ctx.status = 400; - ctx.headers["content-type"] = "text/html; charset=utf-8"; + ctx.headers['content-type'] = 'text/html; charset=utf-8'; ctx.body = ` @@ -72,12 +72,10 @@ router.get("/slack/oauth_redirect", async (ctx) => { const token = await client.openid.connect.token({ client_id: clientId, client_secret: clientSecret, - grant_type: "authorization_code", + grant_type: 'authorization_code', code: ctx.query.code, }); - console.log( - `openid.connect.token response: ${JSON.stringify(token, null, 2)}` - ); + console.log(`openid.connect.token response: ${JSON.stringify(token, null, 2)}`); let userAccessToken = token.access_token; @@ -88,16 +86,10 @@ router.get("/slack/oauth_redirect", async (ctx) => { const refreshedToken = await client.openid.connect.token({ client_id: clientId, client_secret: clientSecret, - grant_type: "refresh_token", + grant_type: 'refresh_token', refresh_token: token.refresh_token, }); - console.log( - `openid.connect.token (refresh) response: ${JSON.stringify( - refreshedToken, - null, - 2 - )}` - ); + console.log(`openid.connect.token (refresh) response: ${JSON.stringify(refreshedToken, null, 2)}`); userAccessToken = refreshedToken.access_token; } @@ -111,11 +103,9 @@ router.get("/slack/oauth_redirect", async (ctx) => { // You don't need to do this here. const tokenWiredClient = new WebClient(userAccessToken); const userInfo = await tokenWiredClient.openid.connect.userInfo(); - console.log( - `openid.connect.userInfo response: ${JSON.stringify(userInfo, null, 2)}` - ); + console.log(`openid.connect.userInfo response: ${JSON.stringify(userInfo, null, 2)}`); - ctx.headers["content-type"] = "text/html; charset=utf-8"; + ctx.headers['content-type'] = 'text/html; charset=utf-8'; ctx.body = ` @@ -130,4 +120,6 @@ router.get("/slack/oauth_redirect", async (ctx) => { // Enable the routes app.use(router.routes()).use(router.allowedMethods()); // Start the web app, which is available at http://localhost:3000/slack/* -app.listen(3000); +app.listen(3000, () => { + console.log('app started'); +});