Skip to content

Commit

Permalink
Implement authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmiliano committed Jul 29, 2022
1 parent 59ca451 commit 940de3e
Show file tree
Hide file tree
Showing 20 changed files with 628 additions and 30 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEVISE_JWT_SECRET_KEY=2d10b194c32b63259ec1c7efbf7846f2789c947d12a77e2911109cd57f667a8e822190c7ce2fe59b874a4af918c25e88ac8f7b1690b069bb28bad4db44ee3aad
26 changes: 6 additions & 20 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,27 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.1.2"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.3", ">= 7.0.3.1"

# Use postgresql as the database for Active Record
gem "pg", "~> 1.1"

# Use Devise for authentication
gem "devise"
gem "devise-jwt"
gem "dotenv-rails", groups: [:development, :test]
gem "rack-cors"

# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
# gem "jbuilder"

# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"

# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"

# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem "rack-cors"

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 ]
gem 'rspec-rails'
end
Expand Down
41 changes: 41 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ GEM
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
bcrypt (3.1.18)
bootsnap (1.12.0)
msgpack (~> 1.2)
builder (3.2.4)
Expand All @@ -74,8 +75,30 @@ GEM
debug (1.6.1)
irb (>= 1.3.6)
reline (>= 0.3.1)
devise (4.8.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
devise-jwt (0.9.0)
devise (~> 4.0)
warden-jwt_auth (~> 0.6)
diff-lcs (1.5.0)
digest (3.1.0)
dotenv (2.8.1)
dotenv-rails (2.8.1)
dotenv (= 2.8.1)
railties (>= 3.2)
dry-auto_inject (0.9.0)
dry-container (>= 0.3.4)
dry-configurable (0.15.0)
concurrent-ruby (~> 1.0)
dry-core (~> 0.6)
dry-container (0.10.0)
concurrent-ruby (~> 1.0)
dry-core (0.8.1)
concurrent-ruby (~> 1.0)
erubi (1.10.0)
factory_bot (6.2.1)
activesupport (>= 5.0.0)
Expand All @@ -91,6 +114,7 @@ GEM
io-console (0.5.11)
irb (1.4.1)
reline (>= 0.3.0)
jwt (2.4.1)
loofah (2.18.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -118,11 +142,14 @@ GEM
nio4r (2.5.8)
nokogiri (1.13.8-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
pg (1.4.1)
puma (5.6.4)
nio4r (~> 2.0)
racc (1.6.0)
rack (2.2.4)
rack-cors (1.1.1)
rack (>= 2.0.0)
rack-test (2.0.2)
rack (>= 1.3)
rails (7.0.3.1)
Expand Down Expand Up @@ -154,6 +181,9 @@ GEM
rake (13.0.6)
reline (0.3.1)
io-console (~> 0.5)
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
Expand All @@ -176,6 +206,13 @@ GEM
timeout (0.3.0)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
warden (1.2.9)
rack (>= 2.0.9)
warden-jwt_auth (0.6.0)
dry-auto_inject (~> 0.8)
dry-configurable (~> 0.13)
jwt (~> 2.1)
warden (~> 1.2)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand All @@ -187,10 +224,14 @@ PLATFORMS
DEPENDENCIES
bootsnap
debug
devise
devise-jwt
dotenv-rails
factory_bot_rails
faker
pg (~> 1.1)
puma (~> 5.0)
rack-cors
rails (~> 7.0.3, >= 7.0.3.1)
rspec-rails
tzinfo-data
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/api/v1/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Api::V1::PostsController < ApplicationController

before_action :authenticate_user!

def index
@posts = Post.all
render json: @posts
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Users::RegistrationsController < Devise::RegistrationsController
respond_to :json

private

def respond_with(resource, _opts = {})
resource.persisted? ? register_success : register_failed
end

def register_success
render json: { message: 'Signed up.' }
end

def register_failed
render json: { message: "Signed up failure." }
end
end
22 changes: 22 additions & 0 deletions app/controllers/users/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Users::SessionsController < Devise::SessionsController
respond_to :json

private

def respond_with(resource, _opts = {})
render json: { message: 'Logged.' }, status: :ok
end

def respond_to_on_destroy
current_user ? log_out_success : log_out_failure
end

def log_out_success
render json: { message: "Logged out." }, status: :ok
end

def log_out_failure
render json: { message: "Logged out failure."}, status: :unauthorized
end
end

5 changes: 5 additions & 0 deletions app/models/jwt_denylist.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class JwtDenylist < ApplicationRecord
include Devise::JWT::RevocationStrategies::Denylist

self.table_name = 'jwt_denylist'
end
13 changes: 13 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable

devise :database_authenticatable,
:jwt_authenticatable,
:trackable,
:registerable,
jwt_revocation_strategy: JwtDenylist

# validates :username, uniqueness: { case_sensitive: false }, presence: true, allow_blank: false, format: { with: /\A[a-zA-Z0-9]+\z/ }

end
5 changes: 5 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ class Application < Rails::Application
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true

config.session_store :cookie_store, key: '_interslice_session'
config.middleware.use ActionDispatch::Cookies
config.middleware.use config.session_store, config.session_options

end
end
3 changes: 3 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@

config.action_mailer.perform_caching = false

# In production, :host should be set to the actual host of the application.
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log

Expand Down
19 changes: 10 additions & 9 deletions config/initializers/cors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

# Read more: https://github.com/cyu/rack-cors

# Rails.application.config.middleware.insert_before 0, Rack::Cors do
# allow do
# origins "example.com"
#
# resource "*",
# headers: :any,
# methods: [:get, :post, :put, :patch, :delete, :options, :head]
# end
# end
# ToDo: We must visit this later..For now, for simplicity we'll allow all origins.
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins "*"

resource "*",
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
Loading

0 comments on commit 940de3e

Please sign in to comment.