Skip to content

Commit cc52898

Browse files
authored
Add Rails 8 authentication (#31)
1 parent 6d1943b commit cc52898

28 files changed

+322
-2
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ gem "stimulus-rails"
1818
gem "jbuilder"
1919

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

2323
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
2424
gem "tzinfo-data", platforms: %i[ windows jruby ]

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ GEM
7676
public_suffix (>= 2.0.2, < 7.0)
7777
ast (2.4.2)
7878
base64 (0.2.0)
79+
bcrypt (3.1.20)
7980
bcrypt_pbkdf (1.1.1)
8081
bcrypt_pbkdf (1.1.1-arm64-darwin)
8182
bcrypt_pbkdf (1.1.1-x86_64-darwin)
@@ -383,6 +384,7 @@ PLATFORMS
383384
x86_64-linux-musl
384385

385386
DEPENDENCIES
387+
bcrypt (~> 3.1.7)
386388
bootsnap
387389
brakeman
388390
capybara
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module ApplicationCable
2+
class Connection < ActionCable::Connection::Base
3+
identified_by :current_user
4+
5+
def connect
6+
set_current_user || reject_unauthorized_connection
7+
end
8+
9+
private
10+
def set_current_user
11+
if session = Session.find_by(id: cookies.signed[:session_id])
12+
self.current_user = session.user
13+
end
14+
end
15+
end
16+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class ApplicationController < ActionController::Base
2+
include Authentication
23
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
34
allow_browser versions: :modern
45
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module Authentication
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
before_action :require_authentication
6+
helper_method :authenticated?
7+
end
8+
9+
class_methods do
10+
def allow_unauthenticated_access(**options)
11+
skip_before_action :require_authentication, **options
12+
end
13+
end
14+
15+
private
16+
def authenticated?
17+
resume_session
18+
end
19+
20+
def require_authentication
21+
resume_session || request_authentication
22+
end
23+
24+
def resume_session
25+
Current.session ||= find_session_by_cookie
26+
end
27+
28+
def find_session_by_cookie
29+
Session.find_by(id: cookies.signed[:session_id]) if cookies.signed[:session_id]
30+
end
31+
32+
def request_authentication
33+
session[:return_to_after_authenticating] = request.url
34+
redirect_to new_session_path
35+
end
36+
37+
def after_authentication_url
38+
session.delete(:return_to_after_authenticating) || root_url
39+
end
40+
41+
def start_new_session_for(user)
42+
user.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |session|
43+
Current.session = session
44+
cookies.signed.permanent[:session_id] = { value: session.id, httponly: true, same_site: :lax }
45+
end
46+
end
47+
48+
def terminate_session
49+
Current.session.destroy
50+
cookies.delete(:session_id)
51+
end
52+
end

app/controllers/home_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class HomeController < ApplicationController
2+
allow_unauthenticated_access(only: :index)
3+
4+
def index
5+
end
6+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class PasswordsController < ApplicationController
2+
allow_unauthenticated_access
3+
before_action :set_user_by_token, only: %i[ edit update ]
4+
5+
def new
6+
end
7+
8+
def create
9+
if user = User.find_by(email: params[:email])
10+
PasswordsMailer.reset(user).deliver_later
11+
end
12+
13+
redirect_to new_session_path, notice: "Password reset instructions sent (if user with that email address exists)."
14+
end
15+
16+
def edit
17+
end
18+
19+
def update
20+
if @user.update(params.permit(:password, :password_confirmation))
21+
redirect_to new_session_path, notice: "Password has been reset."
22+
else
23+
redirect_to edit_password_path(params[:token]), alert: "Passwords did not match."
24+
end
25+
end
26+
27+
private
28+
def set_user_by_token
29+
@user = User.find_by_password_reset_token!(params[:token])
30+
rescue ActiveSupport::MessageVerifier::InvalidSignature
31+
redirect_to new_password_path, alert: "Password reset link is invalid or has expired."
32+
end
33+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class RegistrationsController < ApplicationController
2+
allow_unauthenticated_access
3+
4+
def new
5+
@user = User.new
6+
end
7+
8+
def create
9+
@user = User.new(user_params)
10+
if @user.save
11+
start_new_session_for @user
12+
redirect_to root_path, notice: "Successfully signed up!"
13+
else
14+
render :new
15+
end
16+
end
17+
18+
private
19+
20+
def user_params
21+
params.require(:user).permit(:email, :password, :password_confirmation)
22+
end
23+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class SessionsController < ApplicationController
2+
allow_unauthenticated_access only: %i[ new create ]
3+
rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_url, alert: "Try again later." }
4+
5+
def new
6+
end
7+
8+
def create
9+
if user = User.authenticate_by(params.permit(:email, :password))
10+
start_new_session_for user
11+
redirect_to after_authentication_url
12+
else
13+
redirect_to new_session_path, alert: "Try another email address or password."
14+
end
15+
end
16+
17+
def destroy
18+
terminate_session
19+
redirect_to new_session_path
20+
end
21+
end

app/helpers/home_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module HomeHelper
2+
end

0 commit comments

Comments
 (0)