ueberauth_twitch

An Ueberauth strategy for using Twitch to authenticate your users.


License
MIT

Documentation

Überauth Twitch

Hex Version

Twitch OAuth2 strategy for Überauth.

Installation

  1. Setup your application in Twitch under your profile [applications menu][twitch-apps]

  2. Add :ueberauth_twitch to your list of dependencies in mix.exs:

    def deps do
      [{:ueberauth_twitch, "~> 0.1.0"}]
    end
  3. Add Twitch to your Überauth configuration to your config.exs:

    config :ueberauth, Ueberauth,
      providers: [
        twitch: {Ueberauth.Strategy.Twitch, [default_scope: "user:read:email"]}
      ]
  4. Add your provider configuration to your config.exs next, all this information should mirror what you have for your Twitch app:

    config :ueberauth, Ueberauth.Strategy.Twitch.OAuth,
      client_id: System.get_env("TWITCH_CLIENT_ID"),
      client_secret: System.get_env("TWITCH_CLIENT_SECRET"),
      redirect_uri: System.get_env("TWITCH_REDIRECT_URI")
  5. Include the Üeberauth plug in your router.exs in the browser or custom pipeline:

    defmodule TwitchWeb.Router do
       use TwitchWeb, :router
    
       pipeline :browser do
         plug Ueberauth
       ...
      end
    end
  6. Define the request and callback routes in your router.exs:

    scope "/auth", TwitchWeb do
      pipe_through :browser
    
      get "/:provider", AuthController, :request
      get "/:provider/callback", AuthController, :callback
    end
  7. Create a new controller or use an existing controller that implements callbacks to deal with Ueberauth.Auth and Ueberauth.Failure responses from Twitch.

      defmodule TwitchWeb.AuthController do
        use TwitchWeb, :controller
    
        def callback(%{assigns: %{ueberauth_failure: _fails}} = conn, _params) do
          Logger.debug(_fails)
          conn
          |> put_flash(:error, "Failed to authenticate.")
          |> redirect(to: "/")
        end
    
        def callback(%{assigns: %{ueberauth_auth: auth}} = conn, _params) do
          # This is an example of how you can pass the auth information to
          # a function that you implement that will register or login a user
          case UserFromAuth.find_or_create(auth) do
            {:ok, user} ->
              conn
              |> put_flash(:info, "Successfully authenticated.")
              |> put_session(:current_user, user)
              |> configure_session(renew: true)
              |> redirect(to: "/")
    
            {:error, reason} ->
              conn
              |> put_flash(:error, reason)
              |> redirect(to: "/")
          end
        end
      end

Calling

Once your setup, you can initiate auth using the following URL, unless you changed the routes from the guide:

/auth/twitch

Missing User Email Address

Twitch allows a user to sign up without providing an email address (they can use a phone verify their account). As a result instead of providing an empty string when an email address is not present you will follow the following format: id+login@no-email-provided.twitch.tv