Created
December 22, 2016 22:00
-
-
Save javierg/3e3f32b20c21f8124d5677af908cf9ed to your computer and use it in GitHub Desktop.
sessions_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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