Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small review of code #164

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions lib/spender_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@ defmodule SpenderWeb.AuthController do
Controller to handle ueberauth responses
"""
use SpenderWeb, :controller
plug Ueberauth
plug(Ueberauth)

alias Spender.{Accounts, Accounts.User, Auth.Guardian}
alias Spender.{Accounts, Auth.Guardian}

@doc false
def secret(conn, _params) do
conn |> render("show.json-api", data: %{})
end

# handle callback payload
@doc """
Callback function for federated auth providers to call with identification
information
"""
@spec login(Plug.Conn.t(), any()) :: Plug.Conn.t()
def login(%{assigns: %{ueberauth_auth: auth}} = conn, _params) do
user_params = %{token: auth.credentials.token, firstname: auth.info.first_name, lastname: auth.info.last_name, email: auth.info.email, provider: "google"}
_changeset = User.changeset(%User{}, user_params)
user_params = %{
token: auth.credentials.token,
firstname: auth.info.first_name,
lastname: auth.info.last_name,
email: auth.info.email,
provider: "google"
}

create(conn, user_params)
end

Expand All @@ -24,36 +35,47 @@ defmodule SpenderWeb.AuthController do
|> render("show.json-api", data: %{"error" => "Failed to Authenticate"})
end

# if we can pick a user lets proceed to sign them in and add their details to the session
def create(conn, changeset) do
case insert_or_update_user(changeset) do
@doc """
If This function can pick a user, then lets proceed to sign them in and
add their details to the session. Otherwise it creates a new user provided
validation passes
"""
@spec create(Plug.Conn.t(), map()) :: Plug.Conn.t()
def create(conn, user_params) do
case insert_or_update_user(user_params) do
{:ok, user} ->
#encode a token for current_user
# encode a token for current_user
{:ok, token, _} = Guardian.encode_and_sign(user)

conn
|> put_resp_header("authorization", "Bearer #{token}")
|> Guardian.Plug.sign_in(user)
|> render("show.json-api", data: user)

{:error, _reason} ->
conn
|> put_status(422)
|> render("show.json-api", data: %{"error" => "user"})
end
end

# function to sign user
@doc """
This function to signs a user out of a sesssion using the Guardian.Plug.sign_out()
call
"""
@spec logout(Plug.Conn.t(), any()) :: Plug.Conn.t()
def logout(conn, _) do
conn
|> Guardian.Plug.sign_out()
|> redirect(to: "/")
end



defp insert_or_update_user(%{email: email}=changeset) do
@spec insert_or_update_user(map) :: {:ok, User.t()} | {:error, any()}
defp insert_or_update_user(%{email: email} = map) do
case Accounts.get_by_email(email) do
nil ->
Accounts.create_user(changeset)
Accounts.create_user(map)

user ->
{:ok, user}
end
Expand Down