From a52874c6bdbacd543bfc76766e982cfef40998c5 Mon Sep 17 00:00:00 2001 From: Yuya Date: Sun, 30 Apr 2023 20:50:18 +0900 Subject: [PATCH 1/2] add user model and bcrypt --- Gemfile | 2 ++ Gemfile.lock | 2 ++ app/models/user.rb | 6 ++++++ db/migrate/20230430114757_create_users.rb | 11 +++++++++++ db/schema.rb | 10 +++++++++- spec/factories/users.rb | 7 +++++++ spec/models/user_spec.rb | 5 +++++ 7 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 app/models/user.rb create mode 100644 db/migrate/20230430114757_create_users.rb create mode 100644 spec/factories/users.rb create mode 100644 spec/models/user_spec.rb diff --git a/Gemfile b/Gemfile index 43c1950..2669703 100644 --- a/Gemfile +++ b/Gemfile @@ -35,6 +35,8 @@ gem "bootsnap", require: false gem "graphql" +gem 'bcrypt' + group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem gem "debug", platforms: %i[ mri mingw x64_mingw ] diff --git a/Gemfile.lock b/Gemfile.lock index 16a37ed..4e58fb5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -66,6 +66,7 @@ GEM i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) + bcrypt (3.1.18) bindex (0.8.1) bootsnap (1.16.0) msgpack (~> 1.2) @@ -213,6 +214,7 @@ PLATFORMS x86_64-darwin-21 DEPENDENCIES + bcrypt bootsnap byebug debug diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..13c3e95 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,6 @@ +class User < ApplicationRecord + has_secure_password + + validates :name, presence: true + validates :email, presence: true +end diff --git a/db/migrate/20230430114757_create_users.rb b/db/migrate/20230430114757_create_users.rb new file mode 100644 index 0000000..33b1925 --- /dev/null +++ b/db/migrate/20230430114757_create_users.rb @@ -0,0 +1,11 @@ +class CreateUsers < ActiveRecord::Migration[7.0] + def change + create_table :users do |t| + t.string :name + t.string :email + t.string :password_digest + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 860c7d8..1709e58 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_04_30_085539) do +ActiveRecord::Schema[7.0].define(version: 2023_04_30_114757) do create_table "links", force: :cascade do |t| t.string "url" t.text "description" @@ -18,4 +18,12 @@ t.datetime "updated_at", null: false end + create_table "users", force: :cascade do |t| + t.string "name" + t.string "email" + t.string "password_digest" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + end diff --git a/spec/factories/users.rb b/spec/factories/users.rb new file mode 100644 index 0000000..02771c4 --- /dev/null +++ b/spec/factories/users.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :user do + name { "MyString" } + email { "MyString" } + password_digest { "MyString" } + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 0000000..47a31bb --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe User, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From 1ed6175fd99baa60531259a33092123fc15c195b Mon Sep 17 00:00:00 2001 From: Yuya Date: Sun, 30 Apr 2023 21:21:03 +0900 Subject: [PATCH 2/2] add create_user mutation --- app/graphql/mutations/create_user.rb | 20 +++++++++++++++++++ .../types/auth_provider_credentials_input.rb | 8 ++++++++ app/graphql/types/mutation_type.rb | 1 + app/graphql/types/user_type.rb | 9 +++++++++ app/models/user.rb | 2 +- 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 app/graphql/mutations/create_user.rb create mode 100644 app/graphql/types/auth_provider_credentials_input.rb create mode 100644 app/graphql/types/user_type.rb diff --git a/app/graphql/mutations/create_user.rb b/app/graphql/mutations/create_user.rb new file mode 100644 index 0000000..7279b39 --- /dev/null +++ b/app/graphql/mutations/create_user.rb @@ -0,0 +1,20 @@ +module Mutations + class CreateUser < BaseMutation + 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 diff --git a/app/graphql/types/auth_provider_credentials_input.rb b/app/graphql/types/auth_provider_credentials_input.rb new file mode 100644 index 0000000..9e124b3 --- /dev/null +++ b/app/graphql/types/auth_provider_credentials_input.rb @@ -0,0 +1,8 @@ +module Types + class AuthProviderCredentialsInput < BaseInputObject + graphql_name 'AUTH_PROVIDER_CREDENTIALS' + + argument :email, String, required: true + argument :password, String, required: true + end +end diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index 2374a8f..dc655c9 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -1,5 +1,6 @@ module Types class MutationType < Types::BaseObject + field :create_user, mutation: Mutations::CreateUser field :create_link, mutation: Mutations::CreateLink end end diff --git a/app/graphql/types/user_type.rb b/app/graphql/types/user_type.rb new file mode 100644 index 0000000..f2d2429 --- /dev/null +++ b/app/graphql/types/user_type.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Types + class UserType < Types::BaseObject + field :id, ID, null: false + field :name, String, null: false + field :email, String, null: false + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 13c3e95..d9789f5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,5 +2,5 @@ class User < ApplicationRecord has_secure_password validates :name, presence: true - validates :email, presence: true + validates :email, presence: true, uniqueness: true end