Sign in with Google, GitHub, Slack or Discord

v0.0.3

A site can let people sign in with a Google, GitHub, Slack or Discord account. Each service is a package that adds a button to the sign-in and registration pages and a switch with two key fields to the Security tab of Settings. This guide adds the packages you want, makes an OAuth app at each service and switches the provider on for a site. You finish with a working button.

Before you begin

  • Your account holds admin:dashboard:setting, or a wildcard that covers it. See Permission names.
  • The site is reachable on its own domain, over HTTPS. Slack takes an HTTPS redirect address only.
  • A project cloned from the Mishka CMS repository already loads all four packages. In a host of your own, add the ones you want as shown in the next section.

Every provider is set up per site, so a site that does not use one is not affected by the others.

What each provider adds

ProviderHex packageWhat it addsEmail rule
Googlemishka_cms_auth_googleA Google button and the keys Google client ID and Google client secretGoogle says whether the email is verified, and only a verified one signs in.
GitHubmishka_cms_auth_githubA GitHub button and the keys GitHub client ID and GitHub client secretThe account's primary email is used, and only when GitHub says it is verified.
Slackmishka_cms_auth_slackA Slack button ("Sign in with Slack", OpenID Connect) and the keys Slack client ID and Slack client secretSlack says whether the email is verified, and only a verified one signs in.
Discordmishka_cms_auth_discordA Discord button and the keys Discord client ID and Discord client secretOnly an email Discord has verified signs in. An account with no email does not.

The buttons appear on the sign-in and registration pages the installer writes. A sign-in page you wrote yourself shows them where you place <.sign_in_providers site={assigns[:current_site]} />.

Install a provider or leave it out

Add the packages you want to the dependencies in the host's mix.exs:

elixir
{:mishka_cms_auth_google, "~> 0.0.3"},
{:mishka_cms_auth_github, "~> 0.0.3"},
{:mishka_cms_auth_slack, "~> 0.0.3"},
{:mishka_cms_auth_discord, "~> 0.0.3"}

Run mix deps.get and restart the server. Each package registers itself with Mishka CMS through Mishka Installer. There is no config to add and no migration to run.

To leave a provider out, remove its line from mix.exs and run mix deps.get. Without the package, the provider has no switch on the Security tab and no button on the sign-in page.

Know the redirect address

Each service asks for a redirect address, also called a callback URL. It is the same shape for every provider and every site:

https://<your-site>/api/auth/user/provider/callback

Write the site's own domain in place of <your-site>. A site with a second domain needs its own OAuth app and its own keys.

Create the Google app

  1. Open the Google Cloud console. Under APIs & Services, then Credentials, choose OAuth client ID.
  2. Set the application type to Web application.
  3. Add the redirect address under Authorized redirect URIs.
  4. Copy the Client ID and the Client secret.

While the consent screen is in Testing, add your own account under Test users. Google refuses everyone else until the app is published.

Create the GitHub app

  1. On GitHub, open Settings, then Developer settings, then OAuth Apps, and choose New OAuth App.
  2. Enter the redirect address as the Authorization callback URL.
  3. Copy the Client ID, and make and copy a Client secret.

A GitHub OAuth app takes one callback URL, so make one app for each address.

Create the Slack app

  1. Open api.slack.com/apps and choose Create New App.
  2. Under OAuth & Permissions, add the user scopes openid, email and profile.
  3. In the same place, add the redirect address as a Redirect URL. Slack takes https:// addresses only.
  4. Under Basic Information, in App Credentials, copy the Client ID and the Client Secret.

Create the Discord app

  1. Open discord.com/developers/applications and choose New Application.
  2. Open OAuth2 and add the redirect address under Redirects.
  3. Copy the Client ID. Choose Reset Secret and copy the Client Secret.

The package asks Discord for the identify and email scopes.

Switch the provider on for a site

  1. In the admin sidebar, click Settings and open the Security tab. A master first picks the site in the scope list of the page header.
  2. In the Sign-in methods table, switch the provider on. Its two key fields open under the switch.
  3. Paste the client ID into the first field and the client secret into the second.
  4. Click Save Changes. The screen shows Settings saved.

The client secret is stored sealed and never shown again. The provider's button shows on the sign-in and registration pages once the switch is on and both keys are filled. A site that has not switched a provider on, or has no keys for it, refuses it before the visitor is sent anywhere.

The same table holds Create an account on a first sign-in with a provider, on by default. Switch it off to let a provider sign in only people who already have an account with that email.

What happens when someone signs in

  • A provider signs a person in only when it vouches for the email, as the table above says.
  • An existing confirmed account with the same email is linked to the provider. An account nobody confirmed is claimed.
  • A new account is made only while the site allows it. Closing registration stops it, and so does the switch above. A provider never makes a master.
  • An account with two-step sign-in is asked for its authenticator code after the provider answers.
  • Google and GitHub also use PKCE (S256) on top of the state check.
  • The CMS keeps none of the provider's tokens.

A master signs in on the master site's address with the providers of the master site.

Check the button

  1. Open the site's sign-in page in a private window. The provider's button is there.
  2. Click it, sign in at the service and accept the request. The service sends you back to the site, signed in.

If the service shows a redirect error, compare the address in its console with the redirect address above. They must match exactly, including https and the domain.

Next steps