Ueberauth.Strategy.Github (Üeberauth GitHub v0.8.3) View Source

Provides an Ueberauth strategy for authenticating with GitHub.

Setup

Create an application in Github for you to use.

Register a new application at: your github developer page and get the client_id and client_secret.

Include the provider in your configuration for Ueberauth;

config :ueberauth, Ueberauth,
  providers: [
    github: { Ueberauth.Strategy.Github, [] }
  ]

Then include the configuration for GitHub:

config :ueberauth, Ueberauth.Strategy.Github.OAuth,
  client_id: System.get_env("GITHUB_CLIENT_ID"),
  client_secret: System.get_env("GITHUB_CLIENT_SECRET")

If you haven't already, create a pipeline and setup routes for your callback handler

pipeline :auth do
  Ueberauth.plug "/auth"
end

scope "/auth" do
  pipe_through [:browser, :auth]

  get "/:provider/callback", AuthController, :callback
end

Create an endpoint for the callback where you will handle the Ueberauth.Auth struct:

defmodule MyApp.AuthController do
  use MyApp.Web, :controller

  def callback_phase(%{ assigns: %{ ueberauth_failure: fails } } = conn, _params) do
    # do things with the failure
  end

  def callback_phase(%{ assigns: %{ ueberauth_auth: auth } } = conn, params) do
    # do things with the auth
  end
end

You can edit the behaviour of the Strategy by including some options when you register your provider.

To set the uid_field:

config :ueberauth, Ueberauth,
  providers: [
    github: { Ueberauth.Strategy.Github, [uid_field: :email] }
  ]

Default is :id.

To set the default 'scopes' (permissions):

config :ueberauth, Ueberauth,
  providers: [
    github: { Ueberauth.Strategy.Github, [default_scope: "user,public_repo"] }
  ]

Default is empty ("") which "Grants read-only access to public information (includes public user profile info, public repository info, and gists)"

Link to this section Summary

Functions

Includes the credentials from the GitHub response.

Stores the raw information (including the token) obtained from the GitHub callback.

Cleans up the private area of the connection used for passing the raw GitHub response around during the callback.

Handles the initial redirect to the github authentication page.

Fetches the fields to populate the info section of the Ueberauth.Auth struct.

Fetches the :uid field from the GitHub response.

Link to this section Functions

Includes the credentials from the GitHub response.

Callback implementation for Ueberauth.Strategy.default_options/0.

Stores the raw information (including the token) obtained from the GitHub callback.

Cleans up the private area of the connection used for passing the raw GitHub response around during the callback.

Handles the initial redirect to the github authentication page.

To customize the scope (permissions) that are requested by github include them as part of your url:

"/auth/github?scope=user,public_repo,gist"

Fetches the fields to populate the info section of the Ueberauth.Auth struct.

Fetches the :uid field from the GitHub response.

This defaults to the option :uid_field which in-turn defaults to :id