Skip to content

Commit d62fa09

Browse files
committed
Implement basic login
1 parent c1ed299 commit d62fa09

File tree

21 files changed

+397
-15
lines changed

21 files changed

+397
-15
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
44
ruby "3.4.3"
55

66
gem "rails", "8.0.2"
7+
gem "jsbundling-rails", "1.3.1"
78
gem "bcrypt", "3.1.13"
89
gem "cssbundling-rails", "1.4.3"
910
gem "propshaft", "1.2.1"

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ GEM
147147
jbuilder (2.13.0)
148148
actionview (>= 5.0.0)
149149
activesupport (>= 5.0.0)
150+
jsbundling-rails (1.3.1)
151+
railties (>= 6.0.0)
150152
kamal (2.7.0)
151153
activesupport (>= 7.0)
152154
base64 (~> 0.2)
@@ -357,6 +359,7 @@ DEPENDENCIES
357359
guard-minitest (= 2.4.6)
358360
importmap-rails (= 2.1.0)
359361
jbuilder (= 2.13.0)
362+
jsbundling-rails (= 1.3.1)
360363
kamal (= 2.7.0)
361364
minitest (= 5.25.5)
362365
minitest-reporters (= 1.7.1)

Procfile.dev

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
web: env RUBY_DEBUG_OPEN=true bin/rails server
22
css: yarn watch:css
3+
js: yarn build --watch

app/assets/stylesheets/application.bootstrap.scss

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,21 @@ input {
173173
color: $danger;
174174
}
175175
}
176+
177+
@media (max-width: 800px) {
178+
footer {
179+
small {
180+
display: block;
181+
float: none;
182+
margin-bottom: 1em;
183+
}
184+
ul {
185+
float: none;
186+
padding: 0;
187+
li {
188+
float: none;
189+
margin-left: 0;
190+
}
191+
}
192+
}
193+
}

app/controllers/application_controller.rb

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 SessionsHelper
23
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
34
allow_browser versions: :modern
45

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class SessionsController < ApplicationController
2+
def new
3+
end
4+
5+
def create
6+
user = User.find_by(email: params[:session][:email].downcase)
7+
if user && user.authenticate(params[:session][:password])
8+
reset_session
9+
log_in user
10+
redirect_to user
11+
else
12+
flash.now[:danger] = "Invalid email/password combination"
13+
render "new", status: :unprocessable_entity
14+
end
15+
end
16+
17+
def destroy
18+
log_out
19+
redirect_to root_url, status: :see_other
20+
end
21+
end

app/controllers/users_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ def new
1010
def create
1111
@user = User.new(user_params)
1212
if @user.save
13+
reset_session
14+
log_in @user
1315
flash[:success] = "Welcome to the Sample App!"
1416
redirect_to @user
15-
# Handle a successful save.
1617
else
1718
render "new", status: :unprocessable_entity
1819
end

app/helpers/sessions_helper.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module SessionsHelper
2+
# Logs in the given user.
3+
def log_in(user)
4+
session[:user_id] = user.id
5+
end
6+
7+
# Returns the current logged-in user (if any).
8+
def current_user
9+
if session[:user_id]
10+
@current_user ||= User.find_by(id: session[:user_id])
11+
end
12+
end
13+
14+
# Returns true if the user is logged in, false otherwise.
15+
def logged_in?
16+
!current_user.nil?
17+
end
18+
19+
# Logs out the current user.
20+
def log_out
21+
reset_session
22+
@current_user = nil
23+
end
24+
end

app/javascript/application.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Entry point for the build script in your package.json
2+
import * as bootstrap from "bootstrap";
3+

app/models/user.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ class User < ApplicationRecord
77
uniqueness: { case_sensitive: false }
88
has_secure_password
99
validates :password, presence: true, length: { minimum: 6 }
10+
11+
# Returns the hash digest of the given string.
12+
def User.digest(string)
13+
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
14+
BCrypt::Password.create(string, cost: cost)
15+
end
1016
end

0 commit comments

Comments
 (0)