-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Insert Time Entry with Auth0.uid Filer Time Entry with Auth0.uid
- Loading branch information
Showing
11 changed files
with
209 additions
and
7 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
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,36 @@ | ||
defmodule DeepWorkHoursWeb.AuthController do | ||
use DeepWorkHoursWeb, :controller | ||
alias DeepWorkHoursWeb.Router.Helpers | ||
|
||
plug Ueberauth | ||
|
||
alias Ueberauth.Strategy.Helpers | ||
|
||
def logout(conn, _params) do | ||
conn | ||
|> put_flash(:info, "You have been logged out!") | ||
|> configure_session(drop: true) | ||
|> redirect(to: "/") | ||
end | ||
|
||
def callback(%{assigns: %{ueberauth_failure: _fails}} = conn, _params) do | ||
conn | ||
|> put_flash(:error, "Failed to authenticate.") | ||
|> redirect(to: "/") | ||
end | ||
|
||
def callback(%{assigns: %{ueberauth_auth: auth}} = conn, _params) do | ||
case UserFromAuth.find_or_create(auth) do | ||
{:ok, user} -> | ||
conn | ||
|> put_flash(:info, "Successfully authenticated as " <> user.name <> ".") | ||
|> put_session(:current_user, user) | ||
|> redirect(to: "/") | ||
{:error, reason} -> | ||
conn | ||
|> put_flash(:error, reason) | ||
|> redirect(to: "/") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
defmodule DeepWorkHoursWeb.PageController do | ||
use DeepWorkHoursWeb, :controller | ||
|
||
plug :secure | ||
|
||
defp secure(conn, _params) do | ||
user = get_session(conn, :current_user) | ||
case user do | ||
nil -> | ||
conn | ||
|> redirect(to: "/auth/auth0") | ||
|> halt | ||
_ -> | ||
conn | ||
|> assign(:current_user, user) | ||
end | ||
end | ||
|
||
def index(conn, _params) do | ||
render(conn, "index.html") | ||
render(conn, "index.html", current_user: get_session(conn, :current_user)) | ||
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
defmodule UserFromAuth do | ||
@moduledoc """ | ||
Retrieve the user information from an auth request | ||
""" | ||
require Logger | ||
require Poison | ||
|
||
alias Ueberauth.Auth | ||
|
||
def find_or_create(%Auth{provider: :identity} = auth) do | ||
case validate_pass(auth.credentials) do | ||
:ok -> | ||
{:ok, basic_info(auth)} | ||
{:error, reason} -> {:error, reason} | ||
end | ||
end | ||
|
||
def find_or_create(%Auth{} = auth) do | ||
{:ok, basic_info(auth)} | ||
end | ||
|
||
# github does it this way | ||
defp avatar_from_auth( | ||
%{ | ||
info: %{ | ||
urls: %{ | ||
avatar_url: image | ||
} | ||
} | ||
} | ||
), do: image | ||
|
||
# facebook does it this way | ||
defp avatar_from_auth( | ||
%{ | ||
info: %{ | ||
image: image | ||
} | ||
} | ||
), do: image | ||
|
||
# default case if nothing matches | ||
defp avatar_from_auth(auth) do | ||
Logger.warn auth.provider <> " needs to find an avatar URL!" | ||
Logger.debug(Poison.encode!(auth)) | ||
nil | ||
end | ||
|
||
defp basic_info(auth) do | ||
%{id: auth.uid, name: name_from_auth(auth), avatar: avatar_from_auth(auth)} | ||
end | ||
|
||
defp name_from_auth(auth) do | ||
if auth.info.name do | ||
auth.info.name | ||
else | ||
name = [auth.info.first_name, auth.info.last_name] | ||
|> Enum.filter(&(&1 != nil and &1 != "")) | ||
|
||
cond do | ||
length(name) == 0 -> auth.info.nickname | ||
true -> Enum.join(name, " ") | ||
end | ||
end | ||
end | ||
|
||
defp validate_pass( | ||
%{ | ||
other: %{ | ||
password: "" | ||
} | ||
} | ||
) do | ||
{:error, "Password required"} | ||
end | ||
|
||
defp validate_pass( | ||
%{ | ||
other: %{ | ||
password: pw, | ||
password_confirmation: pw | ||
} | ||
} | ||
) do | ||
:ok | ||
end | ||
|
||
defp validate_pass( | ||
%{ | ||
other: %{ | ||
password: _ | ||
} | ||
} | ||
) do | ||
{:error, "Passwords do not match"} | ||
end | ||
|
||
defp validate_pass(_), do: {:error, "Password Required"} | ||
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
9 changes: 9 additions & 0 deletions
9
priv/repo/migrations/20200220142616_add_uid_to_time_entries.exs
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,9 @@ | ||
defmodule DeepWorkHours.Repo.Migrations.AddUidToTimeEntries do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table("time_entries") do | ||
add :uid, :string | ||
end | ||
end | ||
end |