Skip to content

Commit

Permalink
create person with avatar, #23
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonLab committed Apr 15, 2020
1 parent 3579966 commit 90f95dd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
5 changes: 3 additions & 2 deletions lib/auth_mvp/people/person.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule AuthMvp.People.Person do
schema "people" do
field :email, :string
field :verified, :boolean, default: false
field :avatar, :string

has_many :sessions, AuthMvp.People.Session
timestamps()
Expand All @@ -16,7 +17,7 @@ defmodule AuthMvp.People.Person do
@doc false
def changeset(person, attrs) do
person
|> cast(attrs, [:email, :verified])
|> validate_required([:email, :verified])
|> cast(attrs, [:email, :verified, :avatar])
|> validate_required([:email, :verified, :avatar])
end
end
7 changes: 3 additions & 4 deletions lib/auth_mvp_web/controllers/github_auth_controller.ex
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
defmodule AuthMvpWeb.GithubAuthController do
use AuthMvpWeb, :controller

@doc """
`index/2` handles the callback from GitHub Auth API redirect.
"""
def index(conn, %{"code" => code, "state" => client}) do
{:ok, profile} = ElixirAuthGithub.github_auth(code)

{:ok, session} = case AuthMvp.People.get_person_by_email(profile.email) do
nil ->
{:ok, person} = AuthMvp.People.create_person(%{email: profile.email})
{:ok, person} = AuthMvp.People.create_person(%{email: profile.email, avatar: profile.avatar_url})
AuthMvp.Email.sendemail(%{email: profile.email, template: "welcome"})
AuthMvp.People.create_session(person)

person ->
AuthMvp.People.create_session(person)
end

jwt = AuthMvp.Token.generate_and_sign!(%{email: profile.email, session: session.id})
jwt = AuthMvp.Token.generate_and_sign!(%{email: profile.email, session: session.id, avatar: profile.avatar})
redirect(conn, external: "#{client}?jwt=#{jwt}")
end
end
5 changes: 2 additions & 3 deletions lib/auth_mvp_web/controllers/google_auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ defmodule AuthMvpWeb.GoogleAuthController do
def index(conn, %{"code" => code} = params) do
{:ok, token} = ElixirAuthGoogle.get_token(code, conn)
{:ok, profile} = ElixirAuthGoogle.get_user_profile(token.access_token)

# get the person by email
{:ok, session} = case AuthMvp.People.get_person_by_email(profile.email) do
nil ->
# Create the person
{:ok, person} = AuthMvp.People.create_person(%{email: profile.email})
{:ok, person} = AuthMvp.People.create_person(%{email: profile.email, avatar: profile.picture})
AuthMvp.Email.sendemail(%{email: profile.email, template: "welcome"})
AuthMvp.People.create_session(person)

person ->
AuthMvp.People.create_session(person)
end

jwt = AuthMvp.Token.generate_and_sign!(%{email: profile.email, session: session.id })
jwt = AuthMvp.Token.generate_and_sign!(%{email: profile.email, session: session.id, avatar: profile.avatar })
redirect(conn, external: "#{params["state"]}?jwt=#{jwt}")
end

Expand Down
9 changes: 9 additions & 0 deletions priv/repo/migrations/20200415142150_avatar.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule AuthMvp.Repo.Migrations.Avatar do
use Ecto.Migration

def change do
alter table(:people) do
add :avatar, :text
end
end
end
2 changes: 1 addition & 1 deletion test/auth_mvp_web/controllers/person_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule AuthMvpWeb.PersonControllerTest do
end

test "Get /person/info returns person email when request header authorization is correct" do
assert {:ok, %Person{} = person} = AuthMvp.People.create_person(%{email: "[email protected]"})
assert {:ok, %Person{} = person} = AuthMvp.People.create_person(%{email: "[email protected]", avatar: ""})
jwt = AuthMvp.Token.generate_and_sign!(%{email: "[email protected]"})

conn =
Expand Down

0 comments on commit 90f95dd

Please sign in to comment.