This file contains hidden or 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
| defmodule YayCorp.API.UserController do | |
| use YayCorp.Web, :controller | |
| use Guardian.Phoenix.Controller | |
| alias YayCorp.{User, UserAuth, Repo} | |
| def show(conn, %{"id" => id}, user_auth, _claims) do | |
| requested_user = Repo.get!(User, id) | |
| # users can only retrieve their own details |
This file contains hidden or 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
| defmodule YayCorp.GuardianSerializer do | |
| @behaviour Guardian.Serializer | |
| alias YayCorp.{Repo, UserAuth} | |
| def for_token(user_auth = %UserAuth{}), do: { :ok, "UserAuth:#{user_auth.id}" } | |
| def for_token(_), do: { :error, "Unknown resource type" } | |
| def from_token("UserAuth:" <> id), do: { :ok, Repo.get(UserAuth, id) } | |
| def from_token(_), do: { :error, "Unknown resource type" } |
This file contains hidden or 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
| config :guardian_db, GuardianDb, | |
| repo: YayCorp.Repo, | |
| sweep_interval: 60 |
This file contains hidden or 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
| defmodule YayCorp.Router do | |
| use YayCorp.Web, :router | |
| pipeline :api do | |
| plug :accepts, ["json"] | |
| end | |
| pipeline :authenticated_api do | |
| plug Guardian.Plug.VerifyHeader, realm: "Bearer" | |
| plug Guardian.Plug.LoadResource |
This file contains hidden or 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
| def anon?(%__MODULE__{user_id: nil}), do: true | |
| def anon?(_), do: false |
This file contains hidden or 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
| schema "user_auths" do | |
| field :method, :string | |
| field :remote_id, :string | |
| field :auth0_id, :string | |
| belongs_to :user, User | |
| has_many :devices, Device | |
| timestamps | |
| end |
This file contains hidden or 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
| schema "users" do | |
| field :phone_number, :string | |
| field :first_name, :string | |
| field :last_name, :string | |
| field :display_name, :string | |
| field :employee_id, :string | |
| field :disabled_at, Ecto.DateTime | |
| field :disabled, :boolean, virtual: true | |
| has_one :user_auth, UserAuth, on_replace: :delete |
This file contains hidden or 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
| defp login_with_auth0_token(conn, decoded_jwt) do | |
| case get_user_auth(decoded_jwt) do | |
| nil -> server_error(conn) | |
| user_auth -> | |
| cond do | |
| UserAuth.anon?(user_auth) -> | |
| authorized_anon(conn, user_auth) | |
| User.enabled?(user_auth.user) -> | |
| authorized_employee(conn, user_auth) | |
| true -> |