I am setting up authentication with Phoenix and Absinthe, using Guardian. Using Phoenix 1.3.
I followed a very helpful tutorial that got me authenticating with Absinthe. Logging is simple. I have this mutation defined in my schema:
@desc "Log in"
field :login, type: :session do
arg :email, non_null(:string)
arg :password, non_null(:string)
resolve &UserResolver.login/2
end
…which points to this resolver function, which leaves me with a JWT that I store in a header:
def login(params, _info) do
with {:ok, user} <- Session.authenticate(params, Repo),
{:ok, jwt, _ } <- Guardian.encode_and_sign(user, :access) do
{:ok, %{token: jwt}}
end
end
Now I would like to define another schema that allows me to log out. Before using Absinthe, I did this by manipulating the conn
param in my SessionsController
like this:
def delete(conn, _) do
{:ok, claims} = Guardian.Plug.claims(conn)
conn
|> Guardian.Plug.current_token
|> Guardian.revoke!(claims)
|> render("delete.json")
end
However, I don’t know how to correctly access the conn
variable (or the claims, or token) from within Absinthe’s framework. If I set up a logout
mutation:
@desc "Log out"
field :logout, type: :session do
resolve &UserResolver.logout/2
end
…and a logout
resolver function, the first parameter would refer to the args
(none here) and the second to info
, which seems to be a big graphql object.
This is hideously wrong, but I would like to do something like this:
def logout(_args, info) do
conn = somehowGetAccessToConn
{:ok, claims} = Guardian.Plug.claims(conn)
{:ok, %{message: "Logged out"}} = Guardian.revoke!(conn, claims)
end
How does this work?