https://github.com/khrj/slack-oauth
Setup the OAuth flow for Slack apps easily. Deno port of @slack/oauth
https://github.com/khrj/slack-oauth
deno oauth slack slack-api
Last synced: 2 months ago
JSON representation
Setup the OAuth flow for Slack apps easily. Deno port of @slack/oauth
- Host: GitHub
- URL: https://github.com/khrj/slack-oauth
- Owner: khrj
- License: mit
- Created: 2021-02-10T15:13:56.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-07-12T13:36:08.000Z (almost 5 years ago)
- Last Synced: 2026-01-24T11:14:00.609Z (5 months ago)
- Topics: deno, oauth, slack, slack-api
- Language: TypeScript
- Homepage: https://deno.land/x/slack_oauth
- Size: 80.1 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### Note on Compatiblity
This is module is mostly compatible with its [node
counterpart](https://www.npmjs.com/package/@slack/oauth). However,
`InstallProvider` does not contain a `handleCallback` function taking request
and response handlers. Instead, it has a `handle` function which takes a `code`
and a `state` and does not handle responding and returns a promise which
resolves / rejects to the default HTML for successes / errors, which you can
then respond with or override. This is to maximize compatiblity with various
Deno HTTP servers.
## Usage
### Handling requests
The following is an example of handling a request using [Oak](https://github.com/oakserver/oak) as the HTTP server
```ts
import "https://deno.land/x/dotenv@v2.0.0/load.ts"
import { nanoid } from "https://deno.land/x/nanoid@v3.0.1/mod.ts"
import { Application, Router } from "https://deno.land/x/oak/mod.ts"
import { InstallProvider } from "https://deno.land/x/slack_oauth@3.0.2/mod.ts"
// initialize the installProvider
const installer = new InstallProvider({
clientId: Deno.env.get("SLACK_CLIENT_ID")!,
clientSecret: Deno.env.get("SLACK_CLIENT_SECRET")!,
stateSecret: nanoid(),
})
const router = new Router()
router.get("/", async (ctx) => {
ctx.response.redirect(
await installer.generateInstallUrl({
scopes: ["chat:write"],
}),
)
})
router.get("/slack/oauth_redirect", async (ctx) => {
const code = ctx.request.url.searchParams.get("code")
const state = ctx.request.url.searchParams.get("state")
if (!code || !state) {
ctx.response.status = 401
ctx.response.body = "Unauthorized - Missing code or state"
return
}
try {
const successMessage = await installer.handle(code, state)
ctx.response.status = 200
ctx.response.body = successMessage
} catch (e) {
ctx.response.status = 500
ctx.response.body = e.message
}
})
const app = new Application()
app.use(router.routes())
app.use(router.allowedMethods())
await app.listen({ port: 8000 })
```
### Authorization
You can use the the `installer.authorize()` function to fetch data that has been saved in your installation store.
```ts
import { InstallProvider } from "https://deno.land/x/slack_oauth@3.0.2/mod.ts"
const installer = new InstallProvider({
clientId: Deno.env.get("SLACK_CLIENT_ID")!,
clientSecret: Deno.env.get("SLACK_CLIENT_SECRET")!,
stateSecret: nanoid(),
})
const result = await installer.authorize({ teamId: "my-team-ID" })
console.log(result)
```
## API
- Methods are almost identical to the [node @slack/oauth](https://www.npmjs.com/package/@slack/oauth) (see [Note on Compatiblity](#note-on-compatiblity))
- Generated docs are available at https://doc.deno.land/https/deno.land/x/slack_oauth@3.0.2/mod.ts
## Supporters
[](https://github.com/khrj/slack-oauth/stargazers)
[](https://github.com/khrj/slack-oauth/network/members)
## Related
- [Deno Slack SDK](https://github.com/slack-deno/deno-slack-sdk)
- [Deno modules](https://github.com/khrj/deno-modules)