You've already forked algora-tv
mirror of
https://github.com/algora-io/tv.git
synced 2025-06-12 21:57:28 +02:00
initial commit
This commit is contained in:
47
lib/algora_web/controllers/oauth_callback_controller.ex
Normal file
47
lib/algora_web/controllers/oauth_callback_controller.ex
Normal file
@ -0,0 +1,47 @@
|
||||
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
|
Reference in New Issue
Block a user