Skip to content

doomL/slack-app-oauth-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Slack OAuth Integration

This project provides a simple API endpoint for handling Slack OAuth 2.0 authentication and storing incoming webhook URLs for your workspace. The main logic is in api/slack/oauth.js.

Features

  • Handles Slack OAuth 2.0 flow
  • Retrieves and logs incoming webhook URLs, team names, and app/team IDs
  • (Commented) Example code for saving webhooks to a JSON file and SQLite database
  • Redirects users to their Slack workspace after successful authentication

Getting Started

1. Clone the Repository

2. Install Dependencies

npm install

3. Set Environment Variables

Create a .env file in the root directory with the following variables:

SLACK_CLIENT_ID=your_slack_client_id
SLACK_CLIENT_SECRET=your_slack_client_secret
SLACK_REDIRECT_URI=https://yourdomain.com/api/slack/oauth
  • Replace your_slack_client_id and your_slack_client_secret with values from your Slack app settings.
  • Set SLACK_REDIRECT_URI to the public URL of your /api/slack/oauth endpoint.

4. Run the Server

Start your server (e.g., with Next.js or Express, depending on your setup):

npm run dev

Slack App Configuration

  1. Create a Slack App

    • Go to Slack API: Your Apps and click "Create New App".
    • Choose "From scratch" and give your app a name and workspace.
  2. Configure OAuth & Permissions

    • In your app settings, go to OAuth & Permissions.
    • Add a Redirect URL: https://yourdomain.com/api/slack/oauth
    • Click "Save URLs".
    • Add the required OAuth scopes (e.g., incoming-webhook, chat:write, etc.) depending on your app's needs.
  3. Install the App to Your Workspace

    • In the Slack app settings, click "Install App to Workspace".
    • Authorize the app. You will be redirected to your app's /api/slack/oauth endpoint.
  4. Get Your Client ID and Secret

    • In the app settings, under Basic Information, copy the Client ID and Client Secret.
    • Add these to your .env file as shown above.

Usage

  • Direct users to the Slack OAuth URL:
    https://slack.com/oauth/v2/authorize?client_id=YOUR_CLIENT_ID&scope=incoming-webhook&redirect_uri=YOUR_REDIRECT_URI
    
  • After authorization, Slack will redirect to your /api/slack/oauth endpoint with a code parameter.
  • The handler exchanges the code for tokens and (optionally) stores the webhook URL and team info.
  • Users are redirected to their Slack workspace.

Send a test message

curl -X POST -H 'Content-type: application/json' --data '{"text":"TOP!"}' https://hooks.slack.com/services/{{generated_webhook_url}}

Storing Webhook URLs

The code includes commented-out examples for saving webhook URLs to a JSON file and a SQLite database in the data/ directory. Uncomment and adapt these sections as needed for your persistence requirements.


Troubleshooting

  • Ensure your redirect URI matches exactly in both your Slack app settings and your .env file.
  • Check your server logs for errors during the OAuth exchange.
  • Make sure your server is accessible from the public internet for Slack to redirect users.

About

Lightweight Slack OAuth 2.0 handler for capturing incoming webhook URLs. Simple serverless-ready endpoint for workspace authentication and webhook integration.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors