Skip to content

Commit

Permalink
[#144987927] get or create user from oauth response
Browse files Browse the repository at this point in the history
  • Loading branch information
maorleger committed May 13, 2017
1 parent 8ef67e2 commit 0c9199d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ dist/
# generated elm file
web/static/js/main.js

# local setup files
local.sh
# local scripts folder
script/
6 changes: 5 additions & 1 deletion spec/controllers/auth_controller_spec.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Tracker2x2.AuthControllerSpec do
before do
conn =
build_conn()
|> init_test_session(current_user: "test", access_token: "foo", some_other_info: "bar")
|> init_test_session(oauth_email: "test", access_token: "foo", some_other_info: "bar")
{:ok, %{conn: conn}}
end

Expand All @@ -32,6 +32,10 @@ defmodule Tracker2x2.AuthControllerSpec do
# conn = AuthController.callback(conn(), %{"provider" => "test", "code" => "Test Code"})

end

it "creates a user in the database" do
# TODO: check that a new user has been created
end
end

end
2 changes: 1 addition & 1 deletion spec/controllers/elm_controller_spec.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Tracker2x2.ElmControllerSpec do
conn =
conn()
|> assign(:current_user, nil)
|> init_test_session(current_user: nil)
|> init_test_session(oauth_email: nil)
|> fetch_flash
|> ElmController.authenticate({})
expect(conn.halted).to eq(true)
Expand Down
30 changes: 30 additions & 0 deletions spec/oauth/auth_spec.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# defmodule Tracker2x2.AuthSpec do
# import Plug.Test
# use ESpec.Phoenix
# alias Tracker2x2.Auth

# before do
# conn =
# SHOULD BE SOMETHING WITH SESSION
# |> init_test_session(oauth_email: nil)
# end

# let :conn, do: shared[:conn]

# describe "without a signed in user" do
# it "does not assign a user" do
# new_conn = Auth.call(conn, {})
# expect(new_conn.assigns.current_user).to be_nil
# end
# end

# describe "with a signed in user" do
# it "sets up an internal user record" do
# new_conn =
# conn
# |> put_session(:current_user, %{oauth_email: "[email protected]"})
# |> Auth.call({})
# expect(new_conn.assigns.current_user).not_to be_nil
# end
# end
# end
18 changes: 11 additions & 7 deletions web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ defmodule Tracker2x2.AuthController do

def destroy(conn, _params) do
conn
|> delete_session(:current_user)
|> delete_session(:oauth_email)
|> delete_session(:access_token)
|> redirect(to: page_path(conn, :index))
end

def callback(conn, %{"provider" => provider, "code" => code} = params) do
client = get_token!(provider, code)
user = get_user!(provider, client)
%{email: email} = get_user!(provider, client)

conn
|> put_session(:current_user, user)
|> put_session(:oauth_email, email)
|> put_session(:access_token, client.token.access_token)
|> redirect(to: elm_path(conn, :index))
end
Expand All @@ -27,7 +27,7 @@ defmodule Tracker2x2.AuthController do
end

defp authorize_url!("github") do
GitHub.authorize_url!
GitHub.authorize_url!(scope: "user:email")
end

defp authorize_url!(url) do
Expand All @@ -49,11 +49,15 @@ defmodule Tracker2x2.AuthController do
defp get_user!("google", client) do
user_url = "https://www.googleapis.com/plus/v1/people/me/openIdConnect"
%{body: user} = OAuth2.Client.get!(client, user_url)
%{name: user["name"], email: user["email"]}
%{email: user["email"]}
end

defp get_user!("github", client) do
%{body: user} = OAuth2.Client.get!(client, "https://api.github.com/user")
%{name: user["name"], email: user["email"]}
%{body: user_emails} = OAuth2.Client.get!(client, "https://api.github.com/user/emails")
email = case Enum.find(user_emails, fn(email) -> email["primary"] end) do
nil -> nil
record -> record["email"]
end
%{email: email}
end
end
1 change: 0 additions & 1 deletion web/controllers/elm_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule Tracker2x2.ElmController do

def index(conn, _params) do
conn
|> assign(:current_user, get_session(conn, :current_user))
|> render("index.html")
end

Expand Down
16 changes: 15 additions & 1 deletion web/oauth/auth.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
defmodule Tracker2x2.Auth do
import Plug.Conn
alias Tracker2x2.Repo
alias Tracker2x2.User

def init(opts) do
opts
end

def call(conn, _opts) do
conn
|> assign(:current_user, get_session(conn, :current_user))
|> assign(:current_user, get_or_create_user(conn))
end

defp get_or_create_user(conn) do
email = get_session(conn, :oauth_email)
if email do
case Tracker2x2.Repo.get_by(Tracker2x2.User, email: email) do
nil -> %User{email: email}
user -> user
end
|> Tracker2x2.User.changeset
|> Tracker2x2.Repo.insert_or_update
end
end
end
3 changes: 0 additions & 3 deletions web/oauth/google.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ defmodule Google do
end

def get_token!(params \\ [], headers \\ []) do
IO.puts "GOOOOOOOOO"
IO.inspect params
OAuth2.Client.get_token!(client(), Keyword.merge(params, client_secret: client().client_secret))
end

Expand All @@ -31,7 +29,6 @@ defmodule Google do
end

def get_token(client, params, headers) do
IO.puts "in Google:get_token"
client
|> put_header("Accept", "application/json")
|> AuthCode.get_token(params, headers)
Expand Down

0 comments on commit 0c9199d

Please sign in to comment.