-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch authentication to new rails 8.0 base
- Loading branch information
1 parent
498c090
commit 112f044
Showing
6 changed files
with
101 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationController < ActionController::Base | ||
include Authentication | ||
before_action :basic_profile_info | ||
|
||
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. | ||
allow_browser versions: :modern | ||
|
||
protected | ||
|
||
# @return [String,nil] The value of the authorization cookie | ||
def authorization_header | ||
cookies["Authorization"] | ||
end | ||
|
||
# Gets base information about a profile, things that are needed to show the profile info a of a logged-in user | ||
# @return [BasicProfileInfo] if the user is logged in | ||
# @return [nil] if the user is not logged in | ||
def basic_profile_info | ||
@basic_profile_info ||= retro_meet_client.basic_profile_info | ||
rescue RetroMeetClient::UnauthorizedError | ||
flash.now[:warn] = t("forced_log_out") | ||
cookies.delete("Authorization") | ||
terminate_session | ||
redirect_to :root | ||
end | ||
|
||
def retro_meet_client = @retro_meet_client ||= RetroMeetClient.new(authorization_header) | ||
def retro_meet_client = @retro_meet_client ||= RetroMeetClient.new(Current.session) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
# This module can be included in controllers to enable authentication and related methods. | ||
# It will automatically require authentication for all methods on that controller and any children controllers | ||
module Authentication | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :require_authentication | ||
helper_method :authenticated? | ||
end | ||
|
||
class_methods do | ||
# Method that allows some method in a controller to be unauthenticated. Should really only be used for root and sign_in/register actions | ||
def allow_unauthenticated_access(**options) | ||
skip_before_action :require_authentication, **options | ||
end | ||
end | ||
|
||
private | ||
|
||
# Check if there's a current session | ||
def authenticated? | ||
resume_session | ||
end | ||
|
||
# Makes sure that if there's no session, it will require auth | ||
def require_authentication | ||
resume_session || request_authentication | ||
end | ||
|
||
# Sets the current session from the cookie | ||
def resume_session | ||
Current.session ||= find_session_by_cookie | ||
end | ||
|
||
# (renatolond, 2024-11-08) should it be returning something more than the authorization cookie? | ||
# | ||
# @return [String] | ||
def find_session_by_cookie | ||
cookies.signed[:authorization] | ||
end | ||
|
||
# Sets the return url and redirects to login page | ||
# @return [void] | ||
def request_authentication | ||
session[:return_to_after_authenticating] = request.url | ||
redirect_to new_session_path | ||
end | ||
|
||
# Will either return the url the user was at before, or the root | ||
# | ||
# @return [String] The url to go after the login | ||
def after_authentication_url | ||
session.delete(:return_to_after_authenticating) || root_url | ||
end | ||
|
||
# Sets the cookie for the new session | ||
# @return [void] | ||
def start_new_session_for(authorization_token) | ||
# TODO: check expiration time | ||
cookies.signed[:authorization] = { value: authorization_token, httponly: true, same_site: :strict } | ||
end | ||
|
||
# Logs out from retro meet core and removes the session cookie | ||
# @return [void] | ||
def terminate_session | ||
retro_meet_client.sign_out | ||
cookies.delete(:authorization) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
# This class keeps the current session that's used for authentication | ||
class Current < ActiveSupport::CurrentAttributes | ||
attribute :session | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,12 @@ | |
%h3.title.has-text-grey= t('.sign_in') | ||
%p.subtitle.has-text-grey= t('.login_to_proceed') | ||
.box | ||
= form_for(:sessions, url: session_path()) do |f| | ||
= form_with url: session_path do |form| | ||
.field | ||
.control | ||
= f.email_field :email, autofocus: true, autocomplete: 'email', placeholder: '[email protected]', class: 'input is-large' | ||
= form.email_field :email, autofocus: true, autocomplete: 'email', placeholder: '[email protected]', class: 'input is-large' | ||
.field | ||
.control | ||
= f.password_field :password, autocomplete: "current-password", placeholder: 'password', class: 'input is-large' | ||
.field | ||
= f.label :remember_me, class: 'checkbox' do | ||
= f.check_box :remember_me | ||
= t(".remember_me") | ||
= f.submit t(".sign_in"), class: 'button is-block is-info is-large is-fullwidth' | ||
= form.password_field :password, autocomplete: "current-password", placeholder: 'password', class: 'input is-large' | ||
= form.submit t(".sign_in"), class: 'button is-block is-info is-large is-fullwidth' | ||
= render "sessions/shared/links" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters