Skip to content
Draft
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ GraphQL::Auth.configure do |config|
# config.app_url = ENV['APP_URL']

# config.user_type = '::Types::Auth::User'
# config.sign_up_input_type = '::Types::Auth::Inputs::SignUp'
# config.update_account_input_type = '::Types::Auth::Inputs::UpdateAccount'
# config.update_password_input_type = '::Types::Auth::Inputs::UpdatePassword'

# Devise allowed actions
# Don't forget to enable the lockable setting in your Devise user model if you plan on using the lock_account feature
Expand Down
16 changes: 4 additions & 12 deletions app/graphql/mutations/auth/sign_up.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,17 @@
class Mutations::Auth::SignUp < GraphQL::Schema::Mutation
include ::Graphql::TokenHelper

argument :email, String, required: true do
description "New user's email"
end

argument :password, String, required: true do
description "New user's password"
end

argument :password_confirmation, String, required: true do
description "New user's password confirmation"
argument :input, GraphQL::Auth.configuration.sign_up_input_type.constantize, required: true do
description "Sign up input"
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(args)
def resolve(input:)
response = context[:response]
user = User.new args
user = User.new input.to_h

if user.save
generate_access_token(user, response)
Expand Down
16 changes: 4 additions & 12 deletions app/graphql/mutations/auth/update_account.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
# frozen_string_literal: true

class Mutations::Auth::UpdateAccount < GraphQL::Schema::Mutation
argument :current_password, String, required: true do
description "User's current password"
end

argument :password, String, required: true do
description "User's new password"
end

argument :password_confirmation, String, required: true do
description "User's new password confirmation"
argument :input, GraphQL::Auth.configuration.update_account_input_type.constantize, required: true do
description "Update account input"
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(args)
def resolve(input:)
user = context[:current_user]

if user.blank?
Expand All @@ -30,7 +22,7 @@ def resolve(args)
}
end

user.update_with_password args
user.update_without_password input.to_h

if user.errors.any?
{
Expand Down
43 changes: 43 additions & 0 deletions app/graphql/mutations/auth/update_password.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

class Mutations::Auth::UpdatePassword < GraphQL::Schema::Mutation
argument :input, GraphQL::Auth.configuration.update_password_input_type.constantize, required: true do
description "Update password input"
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(input:)
user = context[:current_user]

if user.blank?
return {
errors: [
{ field: :_error, message: I18n.t('devise.failure.unauthenticated') }
],
success: false,
user: nil
}
end

user.update_with_password input.to_h

if user.errors.any?
{
errors: user.errors.messages.map do |field, messages|
{ field: field.to_s.camelize(:lower), message: messages.first.capitalize }
end,
success: false,
user: nil
}
else
{
errors: [],
success: true,
user: user
}
end
end
end
16 changes: 16 additions & 0 deletions app/graphql/types/auth/inputs/sign_up.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Types::Auth::Inputs::SignUp < Types::BaseInputObject
graphql_name 'SignUpInput'
description 'Sign up arguments'

argument :email, String, required: true do
description "New user's email"
end

argument :password, String, required: true do
description "New user's password"
end

argument :password_confirmation, String, required: true do
description "New user's password confirmation"
end
end
8 changes: 8 additions & 0 deletions app/graphql/types/auth/inputs/update_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Types::Auth::Inputs::UpdateAccount < Types::BaseInputObject
graphql_name 'UpdateAccountInput'
description 'Update account arguments'

argument :email, String, required: true do
description "User's email"
end
end
16 changes: 16 additions & 0 deletions app/graphql/types/auth/inputs/update_password.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Types::Auth::Inputs::UpdatePassword < Types::BaseInputObject
graphql_name 'UpdatePasswordInput'
description 'Update password arguments'

argument :current_password, String, required: true do
description "User's current password"
end

argument :password, String, required: true do
description "User's new password"
end

argument :password_confirmation, String, required: true do
description "User's new password confirmation"
end
end
3 changes: 2 additions & 1 deletion app/graphql/types/graphql_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Types::GraphqlAuth

field :forgot_password, mutation: ::Mutations::Auth::ForgotPassword
field :reset_password, mutation: ::Mutations::Auth::ResetPassword
field :update_password, mutation: ::Mutations::Auth::UpdatePassword

field :update_account, mutation: GraphQL::Auth.configuration.update_account_mutation.constantize

Expand All @@ -23,4 +24,4 @@ module Types::GraphqlAuth
if GraphQL::Auth.configuration.allow_unlock_account
field :unlock_account, mutation: Mutations::Auth::UnlockAccount
end
end
end
1 change: 1 addition & 0 deletions app/helpers/graphql/auth_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def current_user

decrypted_token = GraphQL::Auth::JwtManager.decode(authorization_token)
user = User.find_by id: decrypted_token['user']

return nil if user.blank? || account_locked?(user)

# update token if user is found with token
Expand Down
3 changes: 3 additions & 0 deletions lib/generators/graphql_auth/templates/graphql_auth.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ GraphQL::Auth.configure do |config|
# config.app_url = ENV['APP_URL']

# config.user_type = '::Types::Auth::User'
# config.sign_up_input_type = '::Types::Auth::Inputs::SignUp'
# config.update_account_input_type = '::Types::Auth::Inputs::UpdateAccount'
# config.update_password_input_type = '::Types::Auth::Inputs::UpdatePassword'

# Devise allowed actions
# Don't forget to enable the lockable setting in your Devise user model if you plan on using the lock_account feature
Expand Down
6 changes: 6 additions & 0 deletions lib/graphql-auth/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class Configuration
:jwt_secret_key,
:app_url,
:user_type,
:sign_up_input_type,
:update_account_input_type,
:update_password_input_type,
:allow_sign_up,
:allow_lock_account,
:allow_unlock_account,
Expand All @@ -17,6 +20,9 @@ def initialize
@app_url = ENV['APP_URL']

@user_type = '::Types::Auth::User'
@sign_up_input_type = '::Types::Auth::Inputs::SignUp'
@update_account_input_type = '::Types::Auth::Inputs::UpdateAccount'
@update_password_input_type = '::Types::Auth::Inputs::UpdatePassword'

# Devise allowed actions
@allow_sign_up = true
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql-auth/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module GraphQL
module Auth
VERSION = '0.6.1'
end
end
end
9 changes: 6 additions & 3 deletions spec/dummy/config/initializers/graphql_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
# config.app_url = ENV['APP_URL']

# config.user_type = '::Types::Auth::User'
# config.sign_up_input_type = '::Types::Auth::Inputs::SignUp'
# config.update_account_input_type = '::Types::Auth::Inputs::UpdateAccount'
# config.update_password_input_type = '::Types::Auth::Inputs::UpdatePassword'

# Devise allowed actions
# Don't forget to enable the lockable setting in your Devise user model if you plan on using the lock_account feature
config.allow_sign_up = true
# config.allow_sign_up = true
config.allow_lock_account = true
config.allow_unlock_account = true

# Allow custom mutations for signup and update account
# config.sign_up_mutation = '::Mutations::Auth::SignUp'
# config.udpate_account_mutation = '::Mutations::Auth::UpdateAccount'
end
# config.update_account_mutation = '::Mutations::Auth::UpdateAccount'
end
20 changes: 12 additions & 8 deletions spec/graphql/mutations/auth/sign_up_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

let(:query_string) do
<<-GRAPHQL
mutation($email: String!, $password: String!, $passwordConfirmation: String!) {
signUp(email: $email, password: $password, passwordConfirmation: $passwordConfirmation) {
mutation($input: SignUpInput!) {
signUp(input: $input) {
success
user {
email
Expand All @@ -40,9 +40,11 @@
context 'when valid parameters are given' do
let(:variables) do
{
'email' => 'email@example.com',
'password' => 'password',
'passwordConfirmation' => 'password'
'input' => {
'email' => 'email@example.com',
'password' => 'password',
'passwordConfirmation' => 'password'
}
}
end

Expand All @@ -55,9 +57,11 @@
context 'when invalid parameters are given' do
let(:variables) do
{
'email' => 'emailexample.com',
'password' => 'password',
'passwordConfirmation' => 'password2'
'input' => {
'email' => 'email@example.com',
'password' => 'password',
'passwordConfirmation' => 'password2'
}
}
end

Expand Down
Loading