Sign in with Slack
For: an app developer whose users work in Slack workspaces.
What it does: social.slack adds a “Continue with Slack” button on the OpenID Connect endpoints of Slack. Core finds or makes the user by the workspace id and the user id.
Before you rely on this preset, sign in once with a real account.
Before you start
Section titled “Before you start”- A working social sign-in from Sign in with Google and Apple.
- A Slack app with Sign in with Slack and the scopes
openid,profile, andemail, forsocial.slack.
- Add
https://<your api host>/auth/social/slack/callbackto the redirect URLs of the Slack app. - Copy the client id and the client secret to your secret store.
- Set
social.slackwithclientIdandclientSecret. - Link to
/auth/social/slack/start?next=/homefrom the sign-in page.
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
export function createSlackAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string }) { return createAuth({ baseURL: "https://api.example.com", appURL: "https://app.example.com", trustedOrigins: ["https://app.example.com"], secret: deps.secret, storage: deps.storage, mail: deps.mail, social: { // Read the secret from your secret store. The placeholders only build the config. slack: { clientId: "1234567890.1234567890", clientSecret: "slack-client-secret" }, }, });}import { describe, expect, it } from "vitest";import { call, harness } from "../support/harness.ts";import { createSlackAuth } from "./slack.ts";
describe("sign in with Slack", () => { it("sends the person to the provider with the scope and a PKCE challenge", async () => { const auth = createSlackAuth(harness()); const res = await call(auth, "GET", "/auth/social/slack/start?next=/home"); expect(res.status).toBe(302); const to = new URL(res.headers.get("location") ?? ""); expect(to.origin + to.pathname).toBe("https://slack.com/openid/connect/authorize"); expect(to.searchParams.get("scope")).toBe("openid profile email"); expect(to.searchParams.get("redirect_uri")).toBe( "https://api.example.com/auth/social/slack/callback", ); expect(to.searchParams.get("code_challenge_method")).toBe("S256"); });});How it behaves
Section titled “How it behaves”- The scope is
openid profile email. Core reads the profile fromopenid.connect.userInfo. - The subject is
<team_id>:<user_id>, such asT123:U456. Slack says a user id is unique only in its workspace. A user id alone from a second workspace could sign in as another person. - Slack answers an error with status 200 and
ok: false. The token answer and the profile answer must each holdok: true, elseoauth_failed. - A profile with no team id or no user id answers
oauth_failed. - An address counts as verified only with
email_verified: true. An unverified address links no user that exists.
Residuals
Section titled “Residuals”- Under Slack Enterprise Grid, one person can be in two workspaces. Each workspace gives a new subject, so that person gets two accounts.
- Better Auth keeps the user id alone as the subject of a Slack account. A Better Auth
accountrow of the providerslackdoes not match the core subject by itself. - Before you go live, confirm with a real sign-in that the profile holds the team id. Core’s tests use recorded answers.
If it fails
Section titled “If it fails”oauth_failed: the token call or the profile call answeredok: false, or the profile has no team id. Check the client id, the secret, and the scopes.oauth_denied: the person refused at Slack.email_taken_unverified: another user holds the address, and Slack did not verify it.invalid_token: the state is spent or old, or the callback came to another browser. Start again.sign_up_closed: the sign-up rule refused a new user.