Skip to content

Commit 8393062

Browse files
committed
add jwt user auth
1 parent 608669d commit 8393062

File tree

11 files changed

+521
-2
lines changed

11 files changed

+521
-2
lines changed

Gemfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ gem 'graphql-batch'
2929
gem 'dotenv'
3030
gem 'ridgepole'
3131
gem 'seed-fu'
32-
33-
gem 'rubocop', require: false
32+
gem 'devise'
33+
gem 'devise-jwt'
3434

3535
group :development do
3636
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
3737
# gem "spring"
38+
gem 'rubocop', require: false
3839
end

Gemfile.lock

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,34 @@ GEM
6767
minitest (>= 5.1)
6868
tzinfo (~> 2.0)
6969
ast (2.4.2)
70+
bcrypt (3.1.19)
7071
builder (3.2.4)
7172
concurrent-ruby (1.2.2)
7273
crass (1.0.6)
7374
date (3.3.3)
7475
debug (1.8.0)
7576
irb (>= 1.5.0)
7677
reline (>= 0.3.1)
78+
devise (4.9.2)
79+
bcrypt (~> 3.0)
80+
orm_adapter (~> 0.1)
81+
railties (>= 4.1.0)
82+
responders
83+
warden (~> 1.2.3)
84+
devise-jwt (0.11.0)
85+
devise (~> 4.0)
86+
warden-jwt_auth (~> 0.8)
7787
diffy (3.4.2)
7888
dotenv (2.8.1)
89+
dry-auto_inject (1.0.1)
90+
dry-core (~> 1.0)
91+
zeitwerk (~> 2.6)
92+
dry-configurable (1.0.1)
93+
dry-core (~> 1.0, < 2)
94+
zeitwerk (~> 2.6)
95+
dry-core (1.0.0)
96+
concurrent-ruby (~> 1.0)
97+
zeitwerk (~> 2.6)
7998
erubi (1.12.0)
8099
globalid (1.1.0)
81100
activesupport (>= 5.0)
@@ -89,6 +108,7 @@ GEM
89108
irb (1.7.0)
90109
reline (>= 0.3.0)
91110
json (2.6.3)
111+
jwt (2.7.1)
92112
loofah (2.21.3)
93113
crass (~> 1.0.2)
94114
nokogiri (>= 1.12.0)
@@ -114,6 +134,7 @@ GEM
114134
nio4r (2.5.9)
115135
nokogiri (1.15.2-x86_64-darwin)
116136
racc (~> 1.4)
137+
orm_adapter (0.5.0)
117138
parallel (1.23.0)
118139
parser (3.2.2.1)
119140
ast (~> 2.4.1)
@@ -158,6 +179,9 @@ GEM
158179
regexp_parser (2.8.1)
159180
reline (0.3.5)
160181
io-console (~> 0.5)
182+
responders (3.1.0)
183+
actionpack (>= 5.2)
184+
railties (>= 5.2)
161185
rexml (3.2.5)
162186
ridgepole (1.2.0)
163187
activerecord (>= 5.1, < 7.1)
@@ -183,6 +207,13 @@ GEM
183207
tzinfo (2.0.6)
184208
concurrent-ruby (~> 1.0)
185209
unicode-display_width (2.4.2)
210+
warden (1.2.9)
211+
rack (>= 2.0.9)
212+
warden-jwt_auth (0.8.0)
213+
dry-auto_inject (>= 0.8, < 2)
214+
dry-configurable (>= 0.13, < 2)
215+
jwt (~> 2.1)
216+
warden (~> 1.2)
186217
websocket-driver (0.7.5)
187218
websocket-extensions (>= 0.1.0)
188219
websocket-extensions (0.1.5)
@@ -193,6 +224,8 @@ PLATFORMS
193224

194225
DEPENDENCIES
195226
debug
227+
devise
228+
devise-jwt
196229
dotenv
197230
graphql
198231
graphql-batch
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Users::RegistrationsController < Devise::RegistrationsController
2+
respond_to :json
3+
4+
private
5+
6+
def respond_with(resource, _opts = {})
7+
register_success && return if resource.persisted?
8+
9+
register_failed
10+
end
11+
12+
def register_success
13+
render json: { message: 'Signed up sucessfully.' }
14+
end
15+
16+
def register_failed
17+
render json: { message: "Something went wrong." }
18+
end
19+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Users::SessionsController < Devise::SessionsController
2+
respond_to :json
3+
4+
private
5+
6+
def respond_with(resource, _opts = {})
7+
render json: { message: 'You are logged in.' }, status: :ok
8+
end
9+
10+
def respond_to_on_destroy
11+
log_out_success && return if current_user
12+
13+
log_out_failure
14+
end
15+
16+
def log_out_success
17+
render json: { message: "You are logged out." }, status: :ok
18+
end
19+
20+
def log_out_failure
21+
render json: { message: "Hmm nothing happened."}, status: :unauthorized
22+
end
23+
end

app/graphql/input_types/book.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class InputTypes::Book < Types::BaseInputObject
2+
graphql_name 'BookAttributes' # ObjectTypes::Bookと名前がバッティングしないようにする
3+
4+
argument :title, String, required: true
5+
argument :discription, String, required: true
6+
end

app/models/user.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class User < ApplicationRecord
2+
include Devise::JWT::RevocationStrategies::JTIMatcher
3+
4+
# Include default devise modules. Others available are:
5+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
6+
devise :database_authenticatable, :registerable,
7+
:rememberable, :validatable,
8+
:jwt_authenticatable, jwt_revocation_strategy: self
9+
end

0 commit comments

Comments
 (0)