-
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.
- Loading branch information
Juan Pablo Gil
committed
Oct 26, 2022
1 parent
a757f9b
commit 8f6d400
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
lib/generators/auth/templates/controllers/application_controller.rb
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,30 @@ | ||
# frozen_string_literal: true | ||
|
||
#= Application Controller | ||
# | ||
# Base controller for all other controllers. | ||
class ApplicationController < ActionController::Base | ||
helper_method :current_user, :user_signed_in? | ||
|
||
def warden | ||
request.env['warden'] | ||
end | ||
|
||
def user_signed_in?(...) | ||
warden.authenticated?(...) | ||
end | ||
|
||
def authenticate_user!(...) | ||
session[:after_sign_in_path] = request.path unless user_signed_in?(...) | ||
warden.authenticate!(...) | ||
end | ||
|
||
def after_sign_in_path | ||
session.delete(:after_sign_in_path) || root_path | ||
end | ||
|
||
def current_user(...) | ||
warden.user(...) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
#= HomeController | ||
# | ||
# Handles the home or start page actions. | ||
class HomeController < ApplicationController | ||
def show; end | ||
end | ||
|
43 changes: 43 additions & 0 deletions
43
lib/generators/auth/templates/controllers/session_controller.rb
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,43 @@ | ||
# frozen_string_literal: true | ||
|
||
#= SessionController | ||
# | ||
# Handles the user session actions. | ||
class SessionController < ApplicationController | ||
def new; end | ||
|
||
def create | ||
token = IdentityPlatform::Token.load session_params[:token] | ||
if token.valid? && sign_in_token_user(token) | ||
redirect_to session.fetch :after_sign_in_path, root_path | ||
else | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
sign_out_user | ||
redirect_to new_session_path | ||
end | ||
|
||
private | ||
|
||
def session_params | ||
params.require(:session).permit :token | ||
end | ||
|
||
def sign_in_token_user(token, scope: :default) | ||
user = User.from_identity_token token | ||
warden.set_user(user, scope: scope) | ||
end | ||
|
||
def sign_out_user(scope: nil) | ||
if scope | ||
warden.logout(scope) | ||
warden.clear_strategies_cache!(scope: scope) | ||
else | ||
warden.logout | ||
warden.clear_strategies_cache! | ||
end | ||
end | ||
end |
32 changes: 32 additions & 0 deletions
32
lib/generators/auth/templates/controllers/unauthorized_controller.rb
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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
# UnauthorizedController | ||
# | ||
# The controller configured to be used by Warden to deal whenever the | ||
# authentication fails - either by harshly stopping with an HTTP 401 Unauthorized | ||
# status, or redirecting to the sign-in page. | ||
class UnauthorizedController < ActionController::Metal | ||
include ActionController::Head | ||
include ActionController::Redirecting | ||
include Rails.application.routes.url_helpers | ||
|
||
cattr_accessor :navigational_formats, default: ['*/*', :html] | ||
|
||
def self.call(env) | ||
@respond ||= action(:respond) | ||
@respond.call(env) | ||
end | ||
|
||
def respond | ||
return head :unauthorized unless navigational_format? | ||
|
||
redirect_to sign_in_path, alert: 'You need to sign in before continuing.' | ||
end | ||
|
||
private | ||
|
||
def navigational_format? | ||
request.format.try(:ref).in? navigational_formats | ||
end | ||
end | ||
|