From 2e9e4af4a79e5eaab126f1effb9955ac74586900 Mon Sep 17 00:00:00 2001 From: hassantarif Date: Fri, 19 Aug 2022 16:05:46 +0100 Subject: [PATCH] implementing the sign out functionality --- app/graphql/mutations/auth/sign_out.rb | 33 +++++++++ app/graphql/types/graphql_auth.rb | 3 + spec/graphql/mutations/auth/sign_out_spec.rb | 72 ++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 app/graphql/mutations/auth/sign_out.rb create mode 100644 spec/graphql/mutations/auth/sign_out_spec.rb diff --git a/app/graphql/mutations/auth/sign_out.rb b/app/graphql/mutations/auth/sign_out.rb new file mode 100644 index 0000000..6e68d9a --- /dev/null +++ b/app/graphql/mutations/auth/sign_out.rb @@ -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 \ No newline at end of file diff --git a/app/graphql/types/graphql_auth.rb b/app/graphql/types/graphql_auth.rb index 85bea84..c599ec9 100644 --- a/app/graphql/types/graphql_auth.rb +++ b/app/graphql/types/graphql_auth.rb @@ -16,6 +16,8 @@ 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 @@ -23,4 +25,5 @@ module Types::GraphqlAuth if GraphQL::Auth.configuration.allow_unlock_account field :unlock_account, mutation: Mutations::Auth::UnlockAccount end + end \ No newline at end of file diff --git a/spec/graphql/mutations/auth/sign_out_spec.rb b/spec/graphql/mutations/auth/sign_out_spec.rb new file mode 100644 index 0000000..31e2a48 --- /dev/null +++ b/spec/graphql/mutations/auth/sign_out_spec.rb @@ -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