-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auth: User model and loginUser mutation
- Loading branch information
ztratify
committed
Dec 1, 2020
1 parent
2e6a812
commit d321a7d
Showing
15 changed files
with
204 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Mutations | ||
class CreateUser < BaseMutation | ||
# often we will need input types for specific mutation | ||
# in those cases we can define those input types in the mutation class itself | ||
class AuthProviderSignupData < Types::BaseInputObject | ||
argument :credentials, Types::AuthProviderCredentialsInput, required: false | ||
end | ||
|
||
argument :name, String, required: true | ||
argument :auth_provider, AuthProviderSignupData, required: false | ||
|
||
type Types::UserType | ||
|
||
def resolve(name: nil, auth_provider: nil) | ||
User.create!( | ||
name: name, | ||
email: auth_provider&.[](:credentials)&.[](:email), | ||
password: auth_provider&.[](:credentials)&.[](:password) | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Mutations | ||
class LoginUser < BaseMutation | ||
null true | ||
|
||
argument :credentials, Types::AuthProviderCredentialsInput, required: false | ||
|
||
field :token, String, null: true | ||
field :user, Types::UserType, null: true | ||
|
||
def resolve(credentials: nil) | ||
# basic validation | ||
return unless credentials | ||
|
||
user = User.find_by email: credentials[:email] | ||
|
||
# ensures we have the correct user | ||
return unless user | ||
return unless user.authenticate(credentials[:password]) | ||
|
||
# use Ruby on Rails - ActiveSupport::MessageEncryptor, to build a token | ||
crypt = ActiveSupport::MessageEncryptor.new(Rails.application.credentials.secret_key_base.byteslice(0..31)) | ||
token = crypt.encrypt_and_sign("user-id:#{ user.id }") | ||
|
||
# WARNING: we're storing decrypted tokens on each request! | ||
# Be sure to check out a more secure token method when building a real-world application, such as JWT. | ||
context[:session][:token] = token | ||
|
||
{ user: user, token: token } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Types | ||
class AuthProviderCredentialsInput < BaseInputObject | ||
# the name is usually inferred by class name but can be overwritten | ||
graphql_name 'AUTH_PROVIDER_CREDENTIALS' | ||
|
||
argument :email, String, required: true | ||
argument :password, String, required: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
module Types | ||
class MutationType < BaseObject | ||
field :create_link, mutation: Mutations::CreateLink | ||
field :create_user, mutation: Mutations::CreateUser | ||
field :login_user, mutation: Mutations::LoginUser | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Types | ||
class UserType < Types::BaseObject | ||
field :id, ID, null: false | ||
field :name, String, null: false | ||
field :email, String, null: false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class User < ApplicationRecord | ||
has_secure_password | ||
|
||
validates :name, presence: true | ||
validates :email, presence: true, uniqueness: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateUsers < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :users do |t| | ||
t.string :name | ||
t.string :email | ||
t.string :password_digest | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
name: MyString | ||
email: MyString | ||
password_digest: MyString | ||
|
||
two: | ||
name: MyString | ||
email: MyString | ||
password_digest: MyString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'test_helper' | ||
|
||
class Mutations::CreateUserTest < ActiveSupport::TestCase | ||
def perform(args = {}) | ||
Mutations::CreateUser.new(object: nil, field: nil, context: {}).resolve(args) | ||
end | ||
|
||
test 'create new user' do | ||
user = perform( | ||
name: 'Test User', | ||
auth_provider: { | ||
credentials: { | ||
email: '[email protected]', | ||
password: '[omitted]' | ||
} | ||
} | ||
) | ||
|
||
assert user.persisted? | ||
assert_equal user.name, 'Test User' | ||
assert_equal user.email, '[email protected]' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'test_helper' | ||
|
||
class Mutations::SignInUserTest < ActiveSupport::TestCase | ||
def perform(args = {}) | ||
Mutations::SignInUser.new(object: nil, field: nil, context: { session: {} }).resolve(args) | ||
end | ||
|
||
def create_user | ||
User.create!( | ||
name: 'Test User', | ||
email: '[email protected]', | ||
password: '[omitted]', | ||
) | ||
end | ||
|
||
test 'success' do | ||
user = create_user | ||
|
||
result = perform( | ||
credentials: { | ||
email: user.email, | ||
password: user.password | ||
} | ||
) | ||
|
||
assert result[:token].present? | ||
assert_equal result[:user], user | ||
end | ||
|
||
test 'failure because no credentials' do | ||
assert_nil perform | ||
end | ||
|
||
test 'failure because wrong email' do | ||
create_user | ||
assert_nil perform(credentials: { email: 'wrong' }) | ||
end | ||
|
||
test 'failure because wrong password' do | ||
user = create_user | ||
assert_nil perform(credentials: { email: user.email, password: 'wrong' }) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'test_helper' | ||
|
||
class UserTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |