-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#144987927] get or create user from oauth response
- Loading branch information
Showing
8 changed files
with
64 additions
and
16 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,5 +32,5 @@ dist/ | |
# generated elm file | ||
web/static/js/main.js | ||
|
||
# local setup files | ||
local.sh | ||
# local scripts folder | ||
script/ |
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
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
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
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 |
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
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
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
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 |
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