mirror of
https://github.com/algora-io/tv.git
synced 2025-03-17 20:17:45 +02:00
48 lines
1.4 KiB
Elixir
48 lines
1.4 KiB
Elixir
|
defmodule AlgoraWeb.OAuthCallbackController do
|
||
|
use AlgoraWeb, :controller
|
||
|
require Logger
|
||
|
|
||
|
alias Algora.Accounts
|
||
|
|
||
|
def new(conn, %{"provider" => "github", "code" => code, "state" => state}) do
|
||
|
client = github_client(conn)
|
||
|
|
||
|
with {:ok, info} <- client.exchange_access_token(code: code, state: state),
|
||
|
%{info: info, primary_email: primary, emails: emails, token: token} = info,
|
||
|
{:ok, user} <- Accounts.register_github_user(primary, info, emails, token) do
|
||
|
conn
|
||
|
|> put_flash(:info, "Welcome, #{user.handle}!")
|
||
|
|> AlgoraWeb.UserAuth.log_in_user(user)
|
||
|
else
|
||
|
{:error, %Ecto.Changeset{} = changeset} ->
|
||
|
Logger.debug("failed GitHub insert #{inspect(changeset.errors)}")
|
||
|
|
||
|
conn
|
||
|
|> put_flash(
|
||
|
:error,
|
||
|
"We were unable to fetch the necessary information from your GithHub account"
|
||
|
)
|
||
|
|> redirect(to: "/")
|
||
|
|
||
|
{:error, reason} ->
|
||
|
Logger.debug("failed GitHub exchange #{inspect(reason)}")
|
||
|
|
||
|
conn
|
||
|
|> put_flash(:error, "We were unable to contact GitHub. Please try again later")
|
||
|
|> redirect(to: "/")
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def new(conn, %{"provider" => "github", "error" => "access_denied"}) do
|
||
|
redirect(conn, to: "/")
|
||
|
end
|
||
|
|
||
|
def sign_out(conn, _) do
|
||
|
AlgoraWeb.UserAuth.log_out_user(conn)
|
||
|
end
|
||
|
|
||
|
defp github_client(conn) do
|
||
|
conn.assigns[:github_client] || Algora.Github
|
||
|
end
|
||
|
end
|