defmodule MastheadWeb.SSO do @moduledoc """ Renders the social sign-in buttons, showing a provider only when its OAuth client is actually configured (client id present in the env). Used by the sign-in and sign-up pages. """ use Phoenix.Component use MastheadWeb, :verified_routes @providers [ {:google, "Continue with Google", Ueberauth.Strategy.Google.OAuth}, {:github, "Continue with GitHub", Ueberauth.Strategy.Github.OAuth} ] @doc "Configured providers as `[%{id: atom, label: string}]`." def configured do for {id, label, oauth_mod} <- @providers, configured?(oauth_mod), do: %{id: id, label: label} end defp configured?(oauth_mod) do case Application.get_env(:ueberauth, oauth_mod) do nil -> false cfg -> cfg[:client_id] not in [nil, ""] end end @doc "Divider + one button per configured provider. Nothing if none." def buttons(assigns) do assigns = assign(assigns, :providers, configured()) ~H"""
or
<.provider_icon id={p.id} /> {p.label}
""" end attr :id, :atom, required: true defp provider_icon(%{id: :github} = assigns) do ~H""" """ end defp provider_icon(%{id: :google} = assigns) do ~H""" """ end defp provider_icon(assigns), do: ~H"" end