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 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.