Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Authentication concern #12

Merged
merged 5 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class ApplicationController < ActionController::Base
include Authentication

before_action :authenticate_user!
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,17 @@ module Authentication
extend ActiveSupport::Concern

included do
before_action :current_user

helper_method :current_user
helper_method :user_signed_in?
helper_method :current_user, :user_signed_in?
end

def login(user)
def sign_in(user)
reset_session
session[:current_user_id] = user.id

redirect_to home_path(user.id)
end

def logout
def sign_out
reset_session
end

Expand All @@ -40,16 +37,22 @@ def redirect_if_authenticated
end

def authenticate_user!
redirect_to root_path unless user_signed_in?
if current_user.blank?
redirect_to root_path
end
end

private

def user_signed_in?
current_user.present?
end

def current_user
Current.user ||= session[:current_user_id] && User.find_by(id: session[:current_user_id])
Current.user ||= authenticate_user_from_session
end

def user_signed_in?
Current.user.present?
def authenticate_user_from_session
session[:current_user_id] && User.find_by(id: session[:current_user_id])
end
end
2 changes: 0 additions & 2 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
# limitations under the License.

class HomeController < ApplicationController
before_action :authenticate_user!

def index
end
end
2 changes: 2 additions & 0 deletions app/controllers/landing_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.

class LandingController < ApplicationController
skip_before_action :authenticate_user!

def index
end
end
5 changes: 3 additions & 2 deletions app/controllers/password_reset_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.

class PasswordResetController < ApplicationController
skip_before_action :authenticate_user!
before_action :user_by_token, only: [:edit, :update]

def new
Expand All @@ -23,15 +24,15 @@ def new
def create
User.find_by(email: params[:user][:email])&.password_reset_requested

redirect_to login_path, notice: t("auth.password_reset.message.confirmation")
redirect_to signin_path, notice: t("auth.password_reset.message.confirmation")
end

def edit
end

def update
if @user.update(password_params)
login @user
sign_in @user
else
render :edit, status: :unprocessable_entity
end
Expand Down
23 changes: 15 additions & 8 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,32 @@
# limitations under the License.

class SessionsController < ApplicationController
before_action :redirect_if_authenticated, only: [:create, :new]
skip_before_action :authenticate_user!, only: [:new, :create]
before_action :redirect_if_authenticated, only: :new

def new
end

def create
email = params[:user][:email].strip.downcase
password = params[:user][:password]

if (user = User.authenticate_by(email: email, password: password))
login user
if (user = User.authenticate_by(authentication_params))
sign_in user
else
flash.now[:alert] = t("auth.log_in.error_message")
flash.now[:alert] = t("auth.sign_in.error_message")
render :new, status: :unprocessable_entity
end
end

def destroy
logout
sign_out
redirect_to root_path
end

private

def authentication_params
{
email: params[:user][:email].strip.downcase,
password: params[:user][:password],
}
end
end
3 changes: 2 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
# limitations under the License.

class UsersController < ApplicationController
skip_before_action :authenticate_user!

def create
@user = User.new(user_params)
if @user.save
login @user
sign_in @user
else
render :new, status: :unprocessable_entity
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h1>Welcome <%= current_user.name %></h1>
<%= button_to "Logout", logout_path, method: :delete %>
<%= button_to "Logout", signout_path, method: :delete %>
4 changes: 2 additions & 2 deletions app/views/landing/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<h1 class="text-primary-gradient font-bold"><%= t("app_name") %></h1>
</div>
<div>
<a class="btn-primary" href="<%= login_path %>">
<%= t("auth.login") %>
<a class="btn-primary" href="<%= signin_path %>">
<%= t("auth.signin") %>
</a>
<a class="btn-primary" href="<%= signup_path %>">
<%= t("auth.signup") %>
Expand Down
8 changes: 4 additions & 4 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<div class="max-w-md mx-auto">
<div class="text-center">
<img class="max-w-24 mx-auto mb-4 mt-16" src="<%= asset_path("logo-coloured.svg") %>" alt="Taskodoro logo">
<h2 class="my-4"><%= t("auth.log_in.title") %></h2>
<h2 class="my-4"><%= t("auth.sign_in.title") %></h2>
</div>
<div class="form-container">
<%= form_with url: login_path, scope: :user do |form| %>
<%= form_with url: signin_path, scope: :user do |form| %>
<%= render "shared/form_flash", object: form.object %>
<div>
<%= form.label :email, class: "label" %>
Expand All @@ -15,11 +15,11 @@
<%= form.password_field :password, placeholder: t("auth.placeholder.password"), required: true, class: "input" %>
</div>
<div class="text-center">
<%= form.submit t("auth.login"), class: "btn-primary mx-0 w-full" %>
<%= form.submit t("auth.signin"), class: "btn-primary mx-0 w-full" %>
</div>
<% end %>
</div>
<div class="text-center">
<%= link_to t("auth.log_in.forgot_password"), forgot_password_path, class: "text-sm" %>
<%= link_to t("auth.sign_in.forgot_password"), forgot_password_path, class: "text-sm" %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/users/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
<% end %>
</div>
<div class="text-center text-sm mb-4">
Already have an account. <%= link_to "Log in", login_path %>
<%= t("auth.sign_up.already_signed_up") %> <%= link_to t("auth.signin"), signin_path %>
</div>
</div>
7 changes: 4 additions & 3 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ en:

auth:
signup: "Sign up"
login: "Log in"
signin: "Sign in"

placeholder:
email: "Your email (e.g. [email protected])"
Expand All @@ -45,13 +45,14 @@ en:
title: "Create a new account"
subtitle: "Make the best of your time • Boost your productivity"
headline: "Just need a few things to get you going"
already_signed_up: "Already have an account."

placeholder:
name: "Your name"
password_confirmation: "Confirm your password"

log_in:
title: "Log in to Taskodoro"
sign_in:
title: "Sign in to Taskodoro"
error_message: "Incorrect email or password."
forgot_password: "Forgot your password?"

Expand Down
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
post "signup", to: "users#create"
get "signup", to: "users#new"

post "login", to: "sessions#create"
get "login", to: "sessions#new"
delete "logout", to: "sessions#destroy"
post "signin", to: "sessions#create"
get "signin", to: "sessions#new"
delete "signout", to: "sessions#destroy"

get "forgot_password", to: "password_reset#new"
post "password", to: "password_reset#create"
Expand Down