Skip to content

Instantly share code, notes, and snippets.

@moroz
Created February 3, 2021 14:20
Show Gist options
  • Select an option

  • Save moroz/f6048c1dfafc31dc03e6b79831de7b44 to your computer and use it in GitHub Desktop.

Select an option

Save moroz/f6048c1dfafc31dc03e6b79831de7b44 to your computer and use it in GitHub Desktop.
Set session with Absinthe mutation
defmodule MyAppWeb.Api.Resolvers.SessionResolvers do
alias MyApp.Users
alias MyApp.Users.User
alias MyApp.Token
def sign_in(%{employee_no: employee_no, password: password}, _) do
case Users.authenticate_user_by_employee_no_password(employee_no, password) do
{:ok, %User{} = user} ->
{:ok,
%{success: true, data: %{user: user, access_token: Token.issue_token_for_user(user)}}}
_ ->
{:ok, %{success: false, errors: %{"message" => "您所輸入的電子郵件或密碼有誤。"}}}
end
end
def sign_out(_, _) do
{:ok, %{success: true, user_signed_out: true, user: nil}}
end
end
defmodule MyAppWeb.Api.Types.Sessions do
use Absinthe.Schema.Notation
alias MyAppWeb.Api.Resolvers.SessionResolvers
object :session_mutation_response do
field :success, non_null(:boolean)
field :errors, :json
field :data, :session_data
end
object :session_data do
field :access_token, non_null(:string)
field :user, non_null(:user)
end
object :session_mutations do
field :sign_in, non_null(:session_mutation_response) do
arg(:employee_no, non_null(:string))
arg(:password, non_null(:string))
resolve(&SessionResolvers.sign_in/2)
middleware(MyAppWeb.Api.CookieHelper)
end
field :sign_out, non_null(:session_mutation_response) do
resolve(&SessionResolvers.sign_out/2)
middleware(MyAppWeb.Api.CookieHelper)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment