Skip to content

Instantly share code, notes, and snippets.

@seratch
Last active March 31, 2024 13:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save seratch/92bf98679d7a37a87dfa7376d02a51a1 to your computer and use it in GitHub Desktop.
Save seratch/92bf98679d7a37a87dfa7376d02a51a1 to your computer and use it in GitHub Desktop.
Sign in with Slack - the simplest example

You can run this app by following the steps below:

Set up your Slack app

  • Create a new Slack app from https://api.slack.com/apps?new_app=1
  • Go to OAuth & Permissions
    • Add identity.basic & identity.email to User Token Scopes
    • Add OAuth Redirect URL
  • Go to Basic Information
    • Grab the Client ID & Client Secret in the page

Run the app

export SLACK_CLIENT_ID=
export SLACK_CLIENT_SECRET=
npm i
npx node app.js
const express = require('express');
const app = express();
const port = 3000;
// Display the Sign in with Slack button
app.get('/sign-in', async (_, res) => {
// TODO: adding one-time state parameter as well
const scopes = "identity.basic,identity.email"; // don't mix other non-`identity.` scopes here
// if you already have another Redirect URL for "Add to Slack", you may need to pass redirect_uri as well
const url = `https://slack.com/oauth/v2/authorize?user_scope=${scopes}&client_id=${process.env.SLACK_CLIENT_ID}`;
res.status(200)
.header('Content-Type', 'text/html; charset=utf-8')
// https://api.slack.com/docs/sign-in-with-slack#generator
.send(`<html>
<body>
<a href="${url}"><img alt="Sign in with Slack" height="40" width="172" src="https://platform.slack-edge.com/img/sign_in_with_slack.png" srcset="https://platform.slack-edge.com/img/sign_in_with_slack.png 1x, https://platform.slack-edge.com/img/sign_in_with_slack@2x.png 2x" /></a>
</body>
</html>
`);
});
// Verify the installation
const { WebClient } = require('@slack/web-api');
const client = new WebClient();
app.get('/sign-in-callback', async (req, res) => {
// TODO: verify state parameter
try {
const response = await client.oauth.v2.access({
client_id: process.env.SLACK_CLIENT_ID,
client_secret: process.env.SLACK_CLIENT_SECRET,
code: req.query.code,
});
console.log(response);
const identity = await client.users.identity({
token: response.authed_user.access_token
});
console.log(identity);
// Now you can assume this user logged in with his/her Slack account
// TODO: set-cookie etc
res.status(200).send("You've logged in with your Slack account!");
} catch (e) {
console.log(e);
res.status(500).send("Something wrong!");
}
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
});
{
"name": "sign-in-with-slack-app",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Slack Technologies, Inc.",
"license": "MIT",
"dependencies": {
"@slack/web-api": "^6.2.0-rc.0",
"express": "^4.17.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment