achedeuzot/ueberauth_auth0

Update documentation of hosted page setup

Closed this issue · 2 comments

Hey Sơn,

thanks for your project :)

would like to understand how login with hosted page works. Did not get it working in 2h without help. Could you please help me. Would like to pr an updated documentation afterwards and pr https://github.com/lukeoliff/auth0-elixir-countdown/tree/master/lib/countdown_web to use this auth. As /tokeninfo is a deprecated method and oidc is new auth0 default.

So if i understood everything right from the other issue #3
request phase is now handled through this strategy by default. Added dependency

config :ueberauth, Ueberauth,
   providers: [
      auth0: { Ueberauth.Strategy.Auth0, [] },
   ]
 
  config :ueberauth, Ueberauth.Strategy.Auth0.OAuth,
    domain: System.get_env("AUTH0_DOMAIN"),
    client_id: System.get_env("AUTH0_CLIENT_ID"),
    client_secret: System.get_env("AUTH0_CLIENT_SECRET")

Configured router (in this step i was not 100% sure which method are automatically added through ueberauth / auth0 strategy:

defmodule blaWeb.Router do
  use blaWeb, :router
  require Ueberauth

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/auth", blaWeb do
    pipe_through :browser

   # Tried out with/without them
    get "/:provider", AuthController, :request
    get "/:provider/callback", AuthController, :callback
    #########
  end

  scope "/", blaWeb do
    pipe_through :browser # Use the default browser stack
    get "/", PageController, :index
    post "/", PageController, :sendMail
    resources "/dashboard", SecureController
  end
end
defmodule BlaWeb.AuthController do
  use BlaWeb, :controller
  plug Ueberauth

  alias Ueberauth.Strategy.Helpers

  def delete(conn, _params) do
    conn
    |> put_flash(:info, "You have been logged out!")
    |> configure_session(drop: true)
    |> redirect(to: "/")
  end

  def callback(%{assigns: %{ueberauth_failure: _fails}} = conn, _params) do
    conn
    |> put_flash(:error, "Failed to authenticate.")
    |> redirect(to: "/")
  end

  def callback(%{assigns: %{ueberauth_auth: auth}} = conn, _params) do
    case UserFromAuth.find_or_create(auth) do
      {:ok, user} ->
        conn
        |> put_flash(:info, "Successfully authenticated.")
        |> put_session(:current_user, user)
        |> redirect(to: "/")
      {:error, reason} ->
        conn
        |> put_flash(:error, reason)
        |> redirect(to: "/")
    end
  end
end

Secure controller

defmodule BlaWeb.SecureController do
  use BlaWeb, :controller

  plug Ueberauth

  def index(conn, _params) do
    render conn, "dashboard.html"
  end
end

I do not get it working to send me to hosted page during get /dashboard. Thanks for your help and time :)

Greetings, Thomas

Okay got it working, will create a pr in the next week to document how to do it. get "/:provider", AuthController, :request) was missing in router. Then session can be created with /auth/auth0. Session Information: get_session(conn, :current_user)