niamtokik

niamtokik

Any examples of Phoenix/Ueberauth Module Integration?

Hey guys!

Firstly, I am happy to finally have an account on elixir forum and share my experience with all of you! It’s a real pleasure to be there!

I am currently working on a pretty big project based on Erlang/Elixir (API in Elixir and backend in Erlang). Actually, I am more an Erlang developer than Elixir one, and I have some issue with Ueberauth. I was looking around and did not find any blog post or big example about integrating it with Phoenix (1.4+).

If you don’t have any example or links… Can you give me some feedback about this framework, or other alternative (I tried guardian a little, but don’t have any experience with it).

Many thanks in advance!

Most Liked

cnck1387

cnck1387

OvermindDL1

OvermindDL1

The integration is identical to older Phoenix’s so the existing docs should work fine. I’ve been using ueberauth with phoenix since 1.1 days and I’m on 1.4 still without issue.

If you are getting errors or anything, please post them here and we can help! :slight_smile:

On a side note, if you don’t know or care what JWT is or don’t need to transfer information securely between non-communicating servers via the client, then you don’t need to touch Guardian at all.

alvises

alvises

Hi @niamtokik, welcome to the forum!!

I’ve just started to use this library this week and I had the same feeling - before having a decent understanding of what to do, I had to do quite a bit of research :sweat_smile: I didn’t have clear which steps I had to follow and in which order, especially merging the steps of the library with the customization of the Strategy.

I needed it just to make the user login via a GitHub Oauth app, but as you maybe have seen it’s possible to configure the library with a ton of different services (called Strategies): List of Strategies

So, this is what I did and resources I’ve used.

I’ve found useful the GitHub strategy documentation: Ueberauth.Strategy.Github

1) In my case (GitHub) what I’ve done is to configure Ueberauth in config/config.exs like this

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

In this way I request only personal info default_scope: "user", like name, email and avatar picture. Each strategy has its own default_scope string.

2) After creating the Github app, I’ve configured the strategy with client id and secret, always in the configuration file

# THIS IS A NEW :config
config :ueberauth, Ueberauth.Strategy.Github.OAuth,
  client_id: System.get_env("GITHUB_CLIENT_ID"),
  client_secret: System.get_env("GITHUB_CLIENT_SECRET")

3) Then I created the Phoenix controller that handles the request and callback, like this example

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

  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

plug Ueberauth adds the request phase to the Auth controller, and when the MyApp.AuthController :request action is called, it redirects the user to the GitHub authorization page.

4) To use this controller I’ve then added these routes in router.ex (like the example in the Ueberauth github repo)

scope "/auth", MyApp do
  pipe_through :browser

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

I kept the same default values. In this way, if you make a GET HTTP request with your browser to /auth/github, the :request action is called with "github" provider param, and it redirects you to the GitHub auth page.

Then, when you authorize the GitHub page, you are redirected to /auth/github/callback which triggers callback_phase(%{ assigns: %{ ueberauth_auth: auth } } = conn, params) and you can use auth to get all the data you need.

The example that @sanswork has shared it’s super clear, but let me know if something isn’t clear.. I’d be happy to share part of the code I’ve done so I can be more specific.

Last Post!

cnck1387

cnck1387

I do work on the project, but I’m not a core developer. I am just a random person who stumbled onto the project while learning Phoenix and have sent in a few small pull requests.

I mainly use their code base as a reference guide for my project, although I don’t use the ueberauth feature in my app. Their site supports both signing in with an account you can create with an email address OR you can use a social login instead.

Just glancing at the code in the auth controller, it looks like if you try to sign in with Twitter or something, then ueberauth will determine if the person successfully authenticated and then it pattern matches on the callback to see what to do next.

If it’s bad, the login never happens. If it’s good, it attempts to look up the user by the Twitter handle they used to authenticate. If that was a success, it logs them in. Otherwise it sends them to a /join page to create an initial account with as many fields pre-filled in as possible from the ueberauth data.

The token doesn’t get stored from github or twitter, but info about the user is stored. In either case (logging in with a custom account you created or twitter) the sign_in_and_redirect function gets called in the auth controller which sets up the session and saves the cookie.

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement