Skip to content

Instantly share code, notes, and snippets.

@javierg
Created December 22, 2016 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javierg/3e3f32b20c21f8124d5677af908cf9ed to your computer and use it in GitHub Desktop.
Save javierg/3e3f32b20c21f8124d5677af908cf9ed to your computer and use it in GitHub Desktop.
sessions_controller.ex
defmodule PhoenixUeberauthComeonin.SessionsController do
use PhoenixUeberauthComeonin.Web, :controller
alias Ueberauth.Strategy.Helpers
plug Ueberauth
def new(conn, _params) do
render conn, "new.html", callback_url: Helpers.callback_url(conn), current_user: nil
end
def identity_callback(%{assigns: %{ueberauth_auth: auth}} = conn, params) do
conn
|> authenticated(User.authenticate auth)
end
defp authenticated(conn, {:ok, user}) do
conn
|> put_flash(:info, "Successfully authenticated.")
|> Guardian.Plug.sign_in(user)
|> redirect(to: "/")
end
defp authenticated(conn, {:error, error}) do
conn
|> put_flash(:error, error)
|> redirect(to: "/sessions/new")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment