Modified authentication with user_signed_in? and current_user
This commit is contained in:
@@ -3,10 +3,24 @@ class ApplicationController < ActionController::Base
|
|||||||
before_action :authenticate
|
before_action :authenticate
|
||||||
|
|
||||||
private
|
private
|
||||||
|
def current_user
|
||||||
|
Current.user || authenticate_user_from_session
|
||||||
|
end
|
||||||
|
helper_method :current_user
|
||||||
|
|
||||||
|
def authenticate_user_from_session
|
||||||
|
session_record = Session.find_by_id(cookies.signed[:session_token])
|
||||||
|
Current.session = session_record
|
||||||
|
Current.user
|
||||||
|
end
|
||||||
|
|
||||||
|
def user_signed_in?
|
||||||
|
current_user.present?
|
||||||
|
end
|
||||||
|
helper_method :user_signed_in?
|
||||||
|
|
||||||
def authenticate
|
def authenticate
|
||||||
if session_record = Session.find_by_id(cookies.signed[:session_token])
|
unless user_signed_in?
|
||||||
Current.session = session_record
|
|
||||||
else
|
|
||||||
redirect_to sign_in_path
|
redirect_to sign_in_path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
class HomeController < ApplicationController
|
class HomeController < ApplicationController
|
||||||
|
skip_before_action :authenticate
|
||||||
def index
|
def index
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,23 +1,19 @@
|
|||||||
<p style="color: green"><%= notice %></p>
|
<p style="color: green"><%= notice %></p>
|
||||||
|
<% if user_signed_in? %>
|
||||||
<p>Signed as <%= Current.user.email %></p>
|
<p>Signed as <%= current_user.email %></p>
|
||||||
|
<h2>Login and verification</h2>
|
||||||
<h2>Login and verification</h2>
|
<div>
|
||||||
|
<%= link_to "Change password", edit_password_path %>
|
||||||
<div>
|
</div>
|
||||||
<%= link_to "Change password", edit_password_path %>
|
<div>
|
||||||
</div>
|
<%= link_to "Change email address", edit_identity_email_path %>
|
||||||
|
</div>
|
||||||
<div>
|
<h2>Access history</h2>
|
||||||
<%= link_to "Change email address", edit_identity_email_path %>
|
<div>
|
||||||
</div>
|
<%= link_to "Devices & Sessions", sessions_path %>
|
||||||
|
</div>
|
||||||
<h2>Access history</h2>
|
<br>
|
||||||
|
<%= button_to "Log out", Current.session, method: :delete %>
|
||||||
<div>
|
<% else %>
|
||||||
<%= link_to "Devices & Sessions", sessions_path %>
|
<%= link_to "Log in", sign_in_path %>
|
||||||
</div>
|
<% end %>
|
||||||
|
|
||||||
<br>
|
|
||||||
|
|
||||||
<%= button_to "Log out", Current.session, method: :delete %>
|
|
||||||
|
|||||||
Reference in New Issue
Block a user