Skip to content

Commit eb75708

Browse files
committed
wip first redirect
1 parent 42cbd8c commit eb75708

6 files changed

Lines changed: 103 additions & 5 deletions

File tree

app/controllers/graphql_devise/graphql_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def auth
1515
GraphqlDevise::Schema.execute(params[:query], execute_params(params))
1616
end
1717

18-
render json: result
18+
render json: result unless performed?
1919
end
2020

2121
attr_accessor :client_id, :token, :resource

app/graphql/graphql_devise/mutations/base.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def controller
3131
context[:controller]
3232
end
3333

34+
def set_auth_headers(resource)
35+
auth_headers = resource.create_new_auth_token
36+
response.headers.merge!(auth_headers)
37+
end
38+
3439
def resource_class
3540
context[:resource_class]
3641
end
@@ -39,6 +44,10 @@ def recoverable_enabled?
3944
resource_class.devise_modules.include?(:recoverable)
4045
end
4146

47+
def confirmable_enabled?
48+
resource_class.devise_modules.include?(:confirmable)
49+
end
50+
4251
def current_resource
4352
context[:current_resource]
4453
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module GraphqlDevise
2+
module Mutations
3+
class CheckPasswordToken < Base
4+
argument :reset_password_token, String, required: true
5+
argument :redirect_url, String, required: false
6+
7+
def resolve(reset_password_token:, redirect_url: nil)
8+
resource = resource_class.with_reset_password_token(reset_password_token)
9+
10+
if resource && resource.reset_password_period_valid?
11+
token_info = client_and_token(resource.create_token)
12+
13+
resource.skip_confirmation! if confirmable_enabled? && !resource.confirmed_at
14+
resource.allow_password_change = true if recoverable_enabled?
15+
16+
resource.save!
17+
18+
yield resource if block_given?
19+
20+
redirect_header_options = { reset_password: true }
21+
redirect_headers = controller.send(
22+
:build_redirect_headers,
23+
token_info.fetch(:token),
24+
token_info.fetch(:client_id),
25+
redirect_header_options
26+
)
27+
28+
if redirect_url.present?
29+
controller.redirect_to(resource.build_auth_url(redirect_url, redirect_headers))
30+
else
31+
set_auth_headers(resource)
32+
end
33+
34+
{ authenticable: resource }
35+
else
36+
raise ActionController::RoutingError, 'Not Found'
37+
end
38+
end
39+
40+
private
41+
42+
def client_and_token(token)
43+
if Gem::Version.new(DeviseTokenAuth::VERSION) <= Gem::Version.new('1.1.0')
44+
{ client_id: token.first, token: token.last }
45+
else
46+
{ client_id: token.client, token: token.token }
47+
end
48+
end
49+
end
50+
end
51+
end

lib/graphql_devise/rails/routes.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ def mount_graphql_devise_for(resource, opts = {})
1717
GraphqlDevise::Types::AuthenticableType
1818

1919
default_mutations = {
20-
login: GraphqlDevise::Mutations::Login,
21-
logout: GraphqlDevise::Mutations::Logout,
22-
sign_up: GraphqlDevise::Mutations::SignUp,
23-
update_password: GraphqlDevise::Mutations::UpdatePassword
20+
login: GraphqlDevise::Mutations::Login,
21+
logout: GraphqlDevise::Mutations::Logout,
22+
sign_up: GraphqlDevise::Mutations::SignUp,
23+
update_password: GraphqlDevise::Mutations::UpdatePassword,
24+
check_password_token: GraphqlDevise::Mutations::CheckPasswordToken
2425
}.freeze
2526

2627
default_mutations.each do |action, mutation|
@@ -41,6 +42,7 @@ def mount_graphql_devise_for(resource, opts = {})
4142

4243
devise_scope mapping_name.to_sym do
4344
post "#{path}/graphql_auth", to: 'graphql_devise/graphql#auth'
45+
get "#{path}/graphql_auth", to: 'graphql_devise/graphql#auth'
4446
end
4547
end
4648
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe 'Check Password Token Requests' do
4+
include_context 'with graphql query request'
5+
6+
let(:user) { create(:user, :confirmed) }
7+
let(:redirect_url) { 'https://google.com' }
8+
let(:query) do
9+
<<-GRAPHQL
10+
mutation {
11+
userCheckPasswordToken(
12+
resetPasswordToken: "#{token}",
13+
redirectUrl: "#{redirect_url}"
14+
) {
15+
authenticable { email }
16+
}
17+
}
18+
GRAPHQL
19+
end
20+
21+
context 'when reset password token is valid' do
22+
let(:token) { user.send(:set_reset_password_token) }
23+
24+
context 'when redirect url is provided' do
25+
it 'redirects to redirect url' do
26+
get_request
27+
28+
expect(response).to redirect_to %r{\Ahttps://google.com}
29+
end
30+
end
31+
end
32+
end

spec/support/contexts/graphql_request.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@
1212
def post_request
1313
post '/api/v1/graphql_auth', *graphql_params
1414
end
15+
16+
def get_request
17+
get '/api/v1/graphql_auth', *graphql_params
18+
end
1519
end

0 commit comments

Comments
 (0)