Skip to content

Commit 3cb7458

Browse files
committed
Add js-routes
1 parent 1c751ca commit 3cb7458

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2428
-4
lines changed

Gemfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ gem "puma", ">= 5.0"
1414
gem "jbuilder"
1515

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

1919
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
2020
gem "tzinfo-data", platforms: %i[ windows jruby ]
@@ -42,6 +42,13 @@ gem "vite_rails", "~> 3.0"
4242
# The Rails adapter for Inertia.js [https://inertia-rails.dev]
4343
gem "inertia_rails", "~> 3.10"
4444

45+
# An authentication system generator for Rails applications
46+
# we leave gem here to watch for security updates
47+
gem "authentication-zero"
48+
49+
# Brings Rails named routes to javascript
50+
gem "js-routes"
51+
4552
group :development, :test do
4653
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
4754
gem "debug", platforms: %i[ mri windows ], require: "debug/prelude"

Gemfile.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ GEM
7575
addressable (2.8.7)
7676
public_suffix (>= 2.0.2, < 7.0)
7777
ast (2.4.3)
78+
authentication-zero (4.0.3)
7879
base64 (0.3.0)
80+
bcrypt (3.1.20)
7981
bcrypt_pbkdf (1.1.1)
8082
benchmark (0.4.1)
8183
bigdecimal (3.2.2)
@@ -132,6 +134,9 @@ GEM
132134
jbuilder (2.13.0)
133135
actionview (>= 5.0.0)
134136
activesupport (>= 5.0.0)
137+
js-routes (2.3.5)
138+
railties (>= 5)
139+
sorbet-runtime
135140
json (2.13.2)
136141
kamal (2.7.0)
137142
activesupport (>= 7.0)
@@ -329,6 +334,7 @@ GEM
329334
fugit (~> 1.11.0)
330335
railties (>= 7.1)
331336
thor (>= 1.3.1)
337+
sorbet-runtime (0.5.12368)
332338
sqlite3 (2.7.3-aarch64-linux-gnu)
333339
sqlite3 (2.7.3-aarch64-linux-musl)
334340
sqlite3 (2.7.3-arm-linux-gnu)
@@ -392,13 +398,16 @@ PLATFORMS
392398
x86_64-linux-musl
393399

394400
DEPENDENCIES
401+
authentication-zero
402+
bcrypt (~> 3.1.7)
395403
bootsnap
396404
brakeman
397405
capybara
398406
debug
399407
factory_bot_rails
400408
inertia_rails (~> 3.10)
401409
jbuilder
410+
js-routes
402411
kamal
403412
propshaft
404413
puma (>= 5.0)

Rakefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,13 @@
66
require_relative "config/application"
77

88
Rails.application.load_tasks
9+
10+
if Rails.env.local?
11+
require "rubocop/rake_task"
12+
RuboCop::RakeTask.new
13+
14+
task default: %i[rubocop:autocorrect]
15+
end
16+
17+
# Update js-routes file before javascript build
18+
task "assets:precompile" => "js:routes"
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
# frozen_string_literal: true
2-
31
class ApplicationController < ActionController::Base
42
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
53
allow_browser versions: :modern
4+
5+
before_action :set_current_request_details
6+
before_action :authenticate
7+
8+
private
9+
def authenticate
10+
if session_record = Session.find_by_id(cookies.signed[:session_token])
11+
Current.session = session_record
12+
else
13+
redirect_to sign_in_path
14+
end
15+
end
16+
17+
def set_current_request_details
18+
Current.user_agent = request.user_agent
19+
Current.ip_address = request.ip
20+
end
621
end

app/controllers/home_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class HomeController < ApplicationController
2+
def index
3+
end
4+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Identity::EmailVerificationsController < ApplicationController
2+
skip_before_action :authenticate, only: :show
3+
4+
before_action :set_user, only: :show
5+
6+
def show
7+
@user.update! verified: true
8+
redirect_to root_path, notice: "Thank you for verifying your email address"
9+
end
10+
11+
def create
12+
send_email_verification
13+
redirect_to root_path, notice: "We sent a verification email to your email address"
14+
end
15+
16+
private
17+
def set_user
18+
@user = User.find_by_token_for!(:email_verification, params[:sid])
19+
rescue StandardError
20+
redirect_to edit_identity_email_path, alert: "That email verification link is invalid"
21+
end
22+
23+
def send_email_verification
24+
UserMailer.with(user: Current.user).email_verification.deliver_later
25+
end
26+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Identity::EmailsController < ApplicationController
2+
before_action :set_user
3+
4+
def edit
5+
end
6+
7+
def update
8+
if @user.update(user_params)
9+
redirect_to_root
10+
else
11+
render :edit, status: :unprocessable_content
12+
end
13+
end
14+
15+
private
16+
def set_user
17+
@user = Current.user
18+
end
19+
20+
def user_params
21+
params.permit(:email, :password_challenge).with_defaults(password_challenge: "")
22+
end
23+
24+
def redirect_to_root
25+
if @user.email_previously_changed?
26+
resend_email_verification
27+
redirect_to root_path, notice: "Your email has been changed"
28+
else
29+
redirect_to root_path
30+
end
31+
end
32+
33+
def resend_email_verification
34+
UserMailer.with(user: @user).email_verification.deliver_later
35+
end
36+
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Identity::PasswordResetsController < ApplicationController
2+
skip_before_action :authenticate
3+
4+
before_action :set_user, only: %i[ edit update ]
5+
6+
def new
7+
end
8+
9+
def edit
10+
end
11+
12+
def create
13+
if @user = User.find_by(email: params[:email], verified: true)
14+
send_password_reset_email
15+
redirect_to sign_in_path, notice: "Check your email for reset instructions"
16+
else
17+
redirect_to new_identity_password_reset_path, alert: "You can't reset your password until you verify your email"
18+
end
19+
end
20+
21+
def update
22+
if @user.update(user_params)
23+
redirect_to sign_in_path, notice: "Your password was reset successfully. Please sign in"
24+
else
25+
render :edit, status: :unprocessable_content
26+
end
27+
end
28+
29+
private
30+
def set_user
31+
@user = User.find_by_token_for!(:password_reset, params[:sid])
32+
rescue StandardError
33+
redirect_to new_identity_password_reset_path, alert: "That password reset link is invalid"
34+
end
35+
36+
def user_params
37+
params.permit(:password, :password_confirmation)
38+
end
39+
40+
def send_password_reset_email
41+
UserMailer.with(user: @user).password_reset.deliver_later
42+
end
43+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class PasswordsController < ApplicationController
2+
before_action :set_user
3+
4+
def edit
5+
end
6+
7+
def update
8+
if @user.update(user_params)
9+
redirect_to root_path, notice: "Your password has been changed"
10+
else
11+
render :edit, status: :unprocessable_content
12+
end
13+
end
14+
15+
private
16+
def set_user
17+
@user = Current.user
18+
end
19+
20+
def user_params
21+
params.permit(:password, :password_confirmation, :password_challenge).with_defaults(password_challenge: "")
22+
end
23+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class RegistrationsController < ApplicationController
2+
skip_before_action :authenticate
3+
4+
def new
5+
@user = User.new
6+
end
7+
8+
def create
9+
@user = User.new(user_params)
10+
11+
if @user.save
12+
session_record = @user.sessions.create!
13+
cookies.signed.permanent[:session_token] = { value: session_record.id, httponly: true }
14+
15+
send_email_verification
16+
redirect_to root_path, notice: "Welcome! You have signed up successfully"
17+
else
18+
render :new, status: :unprocessable_content
19+
end
20+
end
21+
22+
private
23+
def user_params
24+
params.permit(:email, :name, :password, :password_confirmation)
25+
end
26+
27+
def send_email_verification
28+
UserMailer.with(user: @user).email_verification.deliver_later
29+
end
30+
end

0 commit comments

Comments
 (0)