Speakeasy v0.3.1 Speakeasy.Authn

Authentication middleware for Absinthe.

See the README for a complete example in a Absinthe Schema.

Link to this section Summary

Functions

Considers the context authenticated if a non-null value is exists under :user_key in the Absinthe.Resolution :context

Link to this section Functions

Link to this function call(res, opts \\ [])

Considers the context authenticated if a non-null value is exists under :user_key in the Absinthe.Resolution :context

Examples

:user_key and :authn_error_message can be set globally and be overwritten per middleware call:

config :speakeasy,
  user_key: :current_user,               # the key the current user will be under in the GraphQL context
  authn_error_message: :unauthenticated  # default authentication

Authenticating using default options:

object :post_mutations do
  @desc "Create post"
  field :create_post, type: :post do
    arg(:name, non_null(:string))
    middleware(Speakeasy.Authn)
  end
end

Authenticating using a custom :user_key

object :post_mutations do
  @desc "Create post"
  field :create_post, type: :post do
    arg(:name, non_null(:string))
    middleware(Speakeasy.Authn, user_key: :user)
  end
end

Authenticating using a string error :message

object :post_mutations do
  @desc "Create post"
  field :create_post, type: :post do
    arg(:name, non_null(:string))
    middleware(Speakeasy.Authn, message: "No way")
  end
end

Authenticating using a callback error :message. This will receive the Absinthe.Resolution :context

object :post_mutations do
  @desc "Create post"
  field :create_post, type: :post do
    arg(:name, non_null(:string))
    middleware(Speakeasy.Authn, message: fn(_ctx) -> "Error message" end)
  end
end