Skip to content

Commit 8182ec4

Browse files
committed
Add Authication API
1 parent 005eab7 commit 8182ec4

File tree

12 files changed

+505
-2
lines changed

12 files changed

+505
-2
lines changed

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ gem "redcarpet"
5656
# It is critical to not include any of the jquery gems when following this pattern or
5757
# else you might have multiple jQuery versions.
5858

59+
gem "devise"
60+
61+
gem 'jwt'
62+
5963
group :development do
6064
# Access an IRB console on exceptions page and /console in development
6165
gem "web-console"

Gemfile.lock

+17
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ GEM
8585
execjs (~> 2)
8686
awesome_print (1.9.2)
8787
base64 (0.2.0)
88+
bcrypt (3.1.20)
8889
benchmark (0.4.0)
8990
bigdecimal (3.1.8)
9091
bindex (0.8.1)
@@ -134,6 +135,12 @@ GEM
134135
irb (~> 1.10)
135136
reline (>= 0.3.8)
136137
debug_inspector (1.2.0)
138+
devise (4.9.4)
139+
bcrypt (~> 3.0)
140+
orm_adapter (~> 0.1)
141+
railties (>= 4.1.0)
142+
responders
143+
warden (~> 1.2.3)
137144
diff-lcs (1.5.1)
138145
docile (1.4.0)
139146
drb (2.2.1)
@@ -165,6 +172,8 @@ GEM
165172
actionview (>= 5.0.0)
166173
activesupport (>= 5.0.0)
167174
json (2.7.2)
175+
jwt (2.10.1)
176+
base64
168177
language_server-protocol (3.17.0.3)
169178
launchy (3.0.1)
170179
addressable (~> 2.8)
@@ -204,6 +213,7 @@ GEM
204213
racc (~> 1.4)
205214
nokogiri (1.16.6-x86_64-linux)
206215
racc (~> 1.4)
216+
orm_adapter (0.5.0)
207217
package_json (0.1.0)
208218
parallel (1.26.3)
209219
parser (3.3.3.0)
@@ -304,6 +314,9 @@ GEM
304314
reline (0.5.9)
305315
io-console (~> 0.5)
306316
require_all (3.0.0)
317+
responders (3.1.1)
318+
actionpack (>= 5.2)
319+
railties (>= 5.2)
307320
rexml (3.3.1)
308321
strscan
309322
rspec-core (3.13.0)
@@ -425,6 +438,8 @@ GEM
425438
unicode-emoji (4.0.4)
426439
uri (1.0.2)
427440
useragent (0.16.10)
441+
warden (1.2.9)
442+
rack (>= 2.0.9)
428443
web-console (4.2.1)
429444
actionview (>= 6.0.0)
430445
activemodel (>= 6.0.0)
@@ -454,10 +469,12 @@ DEPENDENCIES
454469
coveralls_reborn (~> 0.25.0)
455470
database_cleaner
456471
debug (>= 1.0.0)
472+
devise
457473
factory_bot_rails
458474
foreman
459475
generator_spec
460476
jbuilder
477+
jwt
461478
launchy
462479
listen
463480
net-pop!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Api::AuthenticationController < ApplicationController
2+
def create
3+
user = User.find_by(email: params[:email])
4+
5+
if user&.valid_password?(params[:password])
6+
# Generate JWT or session token
7+
token = user.generate_jwt
8+
9+
render json: {
10+
message: 'Login successful',
11+
token: token
12+
}, status: :ok
13+
else
14+
render json: { error: 'Invalid credentials' }, status: :unauthorized
15+
end
16+
end
17+
end

app/controllers/application_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
class ApplicationController < ActionController::Base
44
# Prevent CSRF attacks by raising an exception.
55
# For APIs, you may want to use :null_session instead.
6-
protect_from_forgery with: :exception,
6+
protect_from_forgery with: :null_session,
77
if: proc { request.headers["X-Auth"] != "tutorial_secret" }
88
end

app/models/user.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class User < ApplicationRecord
2+
devise :database_authenticatable, :registerable,
3+
:recoverable, :rememberable, :validatable
4+
5+
def generate_jwt
6+
JWT.encode(
7+
{
8+
id: id,
9+
exp: 60.days.from_now.to_i
10+
},
11+
Rails.application.credentials.secret_key_base
12+
)
13+
end
14+
end

0 commit comments

Comments
 (0)