Skip to content

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.

  1. For GitHub, set social.github to { clientId, clientSecret }. Core asks for the scope read:user user:email.
  2. For LinkedIn, set social.linkedin.
  3. In the LinkedIn app, add the product “Sign In with LinkedIn using OpenID Connect”.
  4. For an OpenID Connect provider, add an entry to social.oidc with its issuer.
  5. Set endpoints on that entry. Then a sign-in does not depend on the provider’s discovery document.
  6. For a provider with no ID token, add an entry to social.oauth2 with a userinfo function.
  7. In userinfo, call the provider with the fetch that core gives.
  8. In the answer of userinfo, set emailVerified: true only for a proved address.
  9. At each provider, register https://api.example.com/auth/social/<name>/callback as the redirect URI.
GitHub, an OIDC entry, and an OAuth2 entry
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,
};
},
},
],
},
});
}
The start of each flow as a test
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",
);
});
});
  • A name matches ^[a-z][a-z0-9-]{0,31}$, is unique across both lists, and is not a preset name, email, or credential. The preset names are google, apple, github, linkedin, microsoft, discord, gitlab, slack, twitter, and facebook.
  • The label is the button text, 1 to 80 characters.
  • An oidc entry with no endpoints reads <issuer>/.well-known/openid-configuration at its first start, and keeps it for the life of the instance.
  • The issuer of that document must equal issuer exactly. A failed read answers oauth_failed and 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 tokenRequest on its social.oauth2 entry. Core runs it after the state checks, and its fetch calls only tokenUrl. Sign in with TikTok and Sign in with WeChat use it.

The failures of Sign in with Google and Apple apply here too. Two more causes:

  • oauth_failed: the userinfo function threw, or did not answer in 10 seconds.
  • oauth_failed: the discovery document has another issuer or a URL that is not https:.

createAuth refuses a bad entry at construction, with a message that names the key.