Configuring the Twitter login process in Phoenix

We're ready to start moving on to actually hooking up the configuration to some real code! The first thing we're going to do in our system is introduced ueberauth and ueberauth_twitter as dependencies to our application. Ueberauth is a handy authentication library that supports logging in via a few different strategies. The idea behind it is that the underlying code that supports the strategies are largely similar, and only a few of the minor details for each need to be modified behind the scenes. This allows us to spend a little bit of up-front development cost to get our first login system working and then spend incredibly little effort getting each additional OAuth provider implemented! Let's start by using ueberauth with an ueberauth_twitter strategy.

First off, in mix.exs, modify the application function to include two new entries for applications to start with our Phoenix application :ueberauth and :ueberauth_twitter:

  def application do
[
mod: {Vocial.Application, []},
extra_applications: [:logger, :runtime_tools, :ueberauth, :ueberauth_twitter]
]
end

Next, still in mix.exs, under the deps function, we're going to add three more libraries to make a part of our Phoenix application: :ueberauth, :ueberauth_twitter, and :oauth:

      {:ueberauth, "~> 0.4"},
{:ueberauth_twitter, "~> 0.2"},
{:oauth, github: "tim/erlang-oauth"}

We've added our dependencies, so now we're ready to pull them into our application! We're going to quickly run the following command in the Terminal:

$ mix do deps.get, compile

After this is successful, we're going to move on to managing the initial configuration for our application. Next, in config.exs, add the following configuration blocks:

config :ueberauth, Ueberauth,
providers: [
twitter: {Ueberauth.Strategy.Twitter, []}
]

config :ueberauth, Ueberauth.Strategy.Twitter.OAuth,
consumer_key: System.get_env("TWITTER_CONSUMER_KEY"),
consumer_secret: System.get_env("TWITTER_CONSUMER_SECRET")

The first configuration block tells the Ueberauth application that it is running as a part of our main application's workflow that one of our Ueberauth providers is going to be called Twitter, and this is going to use the Ueberauth Twitter strategy! After that's in our config, we need to also tell the Twitter OAuth strategy what to use for the consumer_key and the consumer_secret values. This combination helps Twitter understand where our OAuth requests are originating from, which application they should be used with, and how to verify that information. Since we set these values in our environment variables, we'll use System.get_env/1. Let's take a quick look at the help page for this:

iex(1)> h System.get_env

def get_env()

Returns all system environment variables.

The returned value is a map containing name-value pairs. Variable names and
their values are strings.


def get_env(varname)

Returns the value of the given environment variable.

The returned value of the environment variable varname is a string, or nil if
the environment variable is undefined.

Perfect, just what we need! Now, we can start our implementation and write some real code! We'll start off by modifying the users' schema to include some OAuth details so that we can tie this information back to the appropriate accounts and code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset