Sign in with GitHub, LinkedIn, and any OIDC or OAuth2 provider
For: an app developer who wants a provider other than Google and Apple.
What it does: each provider gets a start route and a callback route under /auth/social/<name>/, and a button on core’s sign-in page.
Before you start
Section titled “Before you start”- A working setup from a getting started tutorial.
- An OAuth app at the provider, with its client id and secret. Social lists the keys that hold them.
- For GitHub, set
social.githubto{ clientId, clientSecret }. Core asks for the scoperead:user user:email. - For LinkedIn, set
social.linkedin. - In the LinkedIn app, add the product “Sign In with LinkedIn using OpenID Connect”.
- For an OpenID Connect provider, add an entry to
social.oidcwith itsissuer. - Set
endpointson that entry. Then a sign-in does not depend on the provider’s discovery document. - For a provider with no ID token, add an entry to
social.oauth2with auserinfofunction. - In
userinfo, call the provider with thefetchthat core gives. - In the answer of
userinfo, setemailVerified: trueonly for a proved address. - At each provider, register
https://api.example.com/auth/social/<name>/callbackas the redirect URI.
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
/** The provider secrets. Read them from your secret store. The placeholders only build the config. */export interface ProvidersEnv { GITHUB_CLIENT_ID: string; GITHUB_CLIENT_SECRET: string; ENTRA_TENANT_ID: string; ENTRA_CLIENT_ID: string; ENTRA_CLIENT_SECRET: string; DISCORD_CLIENT_ID: string; DISCORD_CLIENT_SECRET: string;}
const PLACEHOLDERS: ProvidersEnv = { GITHUB_CLIENT_ID: "github-client-id", GITHUB_CLIENT_SECRET: "github-client-secret", ENTRA_TENANT_ID: "00000000-0000-0000-0000-000000000000", ENTRA_CLIENT_ID: "entra-client-id", ENTRA_CLIENT_SECRET: "entra-client-secret", DISCORD_CLIENT_ID: "discord-client-id", DISCORD_CLIENT_SECRET: "discord-client-secret",};
export function createProvidersAuth( deps: { storage: StorageAdapter; mail: MailPort; secret: string }, env: ProvidersEnv = PLACEHOLDERS,) { return createAuth({ baseURL: "https://api.example.com", trustedOrigins: [], secret: deps.secret, storage: deps.storage, mail: deps.mail, social: { // A preset. GitHub issues no ID token, so core reads its user API. github: { clientId: env.GITHUB_CLIENT_ID, clientSecret: env.GITHUB_CLIENT_SECRET }, // An OpenID Connect provider. The Entra entry is an example: core does not test it. // The name `microsoft` belongs to the Entra ID preset `social.microsoft`. oidc: [ { name: "entra", label: "Continue with Microsoft", issuer: `https://login.microsoftonline.com/${env.ENTRA_TENANT_ID}/v2.0`, clientId: env.ENTRA_CLIENT_ID, clientSecret: env.ENTRA_CLIENT_SECRET, // With endpoints, core never reads the discovery document at request time. endpoints: { authorizeUrl: `https://login.microsoftonline.com/${env.ENTRA_TENANT_ID}/oauth2/v2.0/authorize`, tokenUrl: `https://login.microsoftonline.com/${env.ENTRA_TENANT_ID}/oauth2/v2.0/token`, jwksUrl: `https://login.microsoftonline.com/${env.ENTRA_TENANT_ID}/discovery/v2.0/keys`, }, }, ], // A provider that issues no ID token. The Discord entry is an example: core does not test it. // The name `discord` belongs to the Discord preset `social.discord`. oauth2: [ { name: "discord-app", label: "Continue with Discord", authorizeUrl: "https://discord.com/oauth2/authorize", tokenUrl: "https://discord.com/api/oauth2/token", clientId: env.DISCORD_CLIENT_ID, clientSecret: env.DISCORD_CLIENT_SECRET, scope: "identify email", // Call the provider with the fetch that core gives: it stops at 10 s. async userinfo(accessToken, fetch) { const res = await fetch("https://discord.com/api/users/@me", { headers: { authorization: `Bearer ${accessToken}` }, }); if (!res.ok) throw new Error(`discord answered ${res.status}`); const u = (await res.json()) as { id: string; email?: string | null; verified?: boolean; global_name?: string | null; }; return { subject: u.id, email: u.email ?? null, // Only a boolean true from the provider counts as a proof of the address. emailVerified: u.verified === true, name: u.global_name ?? null, image: null, }; }, }, ], }, });}import { describe, expect, it } from "vitest";import { call, harness } from "../support/harness.ts";import { createProvidersAuth } from "./social-providers.ts";
async function startOf(name: string) { const res = await call(createProvidersAuth(harness()), "GET", `/auth/social/${name}/start`); return new URL(res.headers.get("location") ?? "");}
describe("sign in with GitHub and any provider", () => { it("sends GitHub its scope and no nonce", async () => { const to = await startOf("github"); expect(`${to.origin}${to.pathname}`).toBe("https://github.com/login/oauth/authorize"); expect(to.searchParams.get("scope")).toBe("read:user user:email"); expect(to.searchParams.get("nonce")).toBeNull(); });
it("sends an OpenID Connect entry a nonce, and an OAuth 2.0 entry none", async () => { expect((await startOf("entra")).searchParams.get("nonce")).not.toBeNull(); const discord = await startOf("discord-app"); expect(`${discord.origin}${discord.pathname}`).toBe("https://discord.com/oauth2/authorize"); expect(discord.searchParams.get("nonce")).toBeNull(); expect(discord.searchParams.get("redirect_uri")).toBe( "https://api.example.com/auth/social/discord-app/callback", ); });});How it behaves
Section titled “How it behaves”- A
namematches^[a-z][a-z0-9-]{0,31}$, is unique across both lists, and is not a preset name,email, orcredential. The preset names aregoogle,apple,github,linkedin,microsoft,discord,gitlab,slack,twitter, andfacebook. - The
labelis the button text, 1 to 80 characters. - An
oidcentry with noendpointsreads<issuer>/.well-known/openid-configurationat its first start, and keeps it for the life of the instance. - The issuer of that document must equal
issuerexactly. A failed read answersoauth_failedand is not kept. - GitHub counts an address as verified only when its primary address has
verified: true. - LinkedIn’s subject is pairwise for each client id. A new LinkedIn client id gives every person a new subject.
- Every call to a provider stops after 10 seconds. Core never stores or logs a provider’s access token.
- Each of the six newer presets has its own guide: Microsoft, Discord, GitLab, Slack, X, and Facebook.
- A provider with a token exchange of its own can set
tokenRequeston itssocial.oauth2entry. Core runs it after the state checks, and its fetch calls onlytokenUrl. Sign in with TikTok and Sign in with WeChat use it.
If it fails
Section titled “If it fails”The failures of Sign in with Google and Apple apply here too. Two more causes:
oauth_failed: theuserinfofunction threw, or did not answer in 10 seconds.oauth_failed: the discovery document has anotherissueror a URL that is nothttps:.
createAuth refuses a bad entry at construction, with a message that names the key.