Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions app/graphql/mutations/auth/sign_out.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Mutations::Auth::SignOut < GraphQL::Schema::Mutation
include ::Graphql::TokenHelper

argument :refresh_token,String requied: true do
description "refresh token for expiring the user session"
end

field :errors, [::Types::Auth::Error], null: false
field :success, Boolean, null: false
field :user, GraphQL::Auth.configuration.user_type.constantize, null: true

def resolve(refresh_token:)
if (refresh_token.nil? && refresh_token.empty?)
{
success: false,
errors: ["refresh token is invalid"],
user: nil
}
else
user = User.find_by_refresh_token(refresh_token)
delete_refresh_token(user)

{
success: true,
errors: [],
user: nil
}

end

end

end
3 changes: 3 additions & 0 deletions app/graphql/types/graphql_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ module Types::GraphqlAuth

field :validate_token, mutation: ::Mutations::Auth::ValidateToken

field :sign_out, mutation: ::Mutations::Auth::SignOut

if GraphQL::Auth.configuration.allow_lock_account
field :lock_account, mutation: Mutations::Auth::LockAccount
end

if GraphQL::Auth.configuration.allow_unlock_account
field :unlock_account, mutation: Mutations::Auth::UnlockAccount
end

end
72 changes: 72 additions & 0 deletions spec/graphql/mutations/auth/sign_out_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mutations::Auth::SignOut, type: :request do
let!(:user) do
user = User.create!(
email: 'email@example.com',
password: 'password'
)
end

let(:result) do
GraphqlSchema.execute(
query_string,
variables: variables,
context: context
)
end

let(:query_string) do
<<-GRAPHQL
mutation($refresh_token: String!) {
resetPassword(refresh_token: $refresh_token) {
success
errors {
field
message
}
user
}
}
GRAPHQL
end

let(:context) do
{
current_user: nil,
response: ResponseMock.new(headers: {}),
}
end

subject { result }

context "when valid paramters are given" do
let(:variables) do
{
"refresh_token" => user.refresh_token
}
end

it "sign out the user" do
subject

expect(result.dig(["data"]["success"])).to be_truthy
end
end
context "when invalid paramters are given" do
let(:variables) do
{
"refresh_token" => nil
}
end

it "returns error message" do
subject

expect(result.dig(["data"]["success"])).to be_falsey
expect(result.dig(["data"]["user"])).to nil
end
end
end