Last active
May 3, 2018 14:21
-
-
Save ihorkatkov/6303c391299547ac65d8cf79f8cc1803 to your computer and use it in GitHub Desktop.
Absinthe Auth modules
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 Auth.Plug do | |
@moduledoc """ | |
Absinthe authentication plug | |
""" | |
use Phauxth.Authenticate.Base | |
@doc """ | |
Sets current user variable | |
""" | |
def set_user(user, conn) do | |
put_private(conn, :absinthe, %{context: %{current_user: user}}) | |
end | |
end | |
defmodule Web.Schema.Middleware.Authorize do | |
@moduledoc """ | |
Handles authorization | |
""" | |
@behaviour Absinthe.Middleware | |
alias Auth.Accounts.User | |
def call(resolution, _) do | |
authorize(resolution) | |
end | |
defp authorize(%{context: %{current_user: %User{} = _user}} = resolution), do: resolution | |
defp authorize(resolution), | |
do: resolution |> Absinthe.Resolution.put_result({:error, "unauthorized"}) | |
end | |
defmodule Karta.Web.Schema.HouseholdTypes do | |
@moduledoc """ | |
Types for Household context | |
""" | |
object :household_mutations do | |
field :delete_household, :boolean do | |
arg :id, non_null :id | |
middleware( | |
Absinthe.Relay.Node.ParseIDs, | |
id: :household | |
) | |
middleware(Karta.Web.Schema.Middleware.Authorize) | |
resolve(&Resolvers.Household.delete_household/2) | |
end | |
end | |
end | |
defmodule Web.Resolvers.Household do | |
@moduledoc """ | |
Household resolver | |
""" | |
alias DB.Households | |
def delete_household(%{id: id}, %{context: %{current_user: user}}) do | |
Households.delete(household) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment