2017-09-20 5 views
0

コントローラに自分の認証機能を加え、ユーザーがサインインしているかどうかを確認します(マネージャなどの場合)。私が見つけた問題は、サインインしていない人が認証が必要なページにアクセスしようとすると、サインインページにリダイレクトされてしまいましたが、サインインした後に、アクセスしようとしています。Rails 4:サインイン後のデビッドリダイレクト

私はApplicationControllerに以下を追加しました:

before_filter :store_location 

def store_location 
    p "storing location" 
    # store last url - this is needed for post-login redirect to whatever the user last visited. 
    return unless request.get? 
    if (request.path != "https://stackoverflow.com/users/sign_in" && 
     request.path != "https://stackoverflow.com/users/sign_up" && 
     request.path != "https://stackoverflow.com/users/password/new" && 
     request.path != "https://stackoverflow.com/users/password/edit" && 
     request.path != "https://stackoverflow.com/users/confirmation" && 
     request.path != "https://stackoverflow.com/users/sign_out" && 
     !request.xhr?) # don't store ajax calls 
    session[:previous_url] = request.fullpath 
    end 
end 

def after_sign_in_path_for(resource) 
    session["user_return_to"] || root_path 
end 

def new_authorize_employee 
    if current_user.nil? 
     redirect_to new_user_session_path, notice: "You must be signed in to access this page." 
    else 
     unless current_user.is_employee? 
      redirect_to root_path, notice: "You do not have permissions to access this page." 
     end 
    end 
end 

とコントローラにユーザーがアクセスしようとしている:

class EmployeesController < ApplicationController 
    before_action :new_authorize_employee 
    ... 
end 

が、ユーザーがまだサインインした後、root_urlにリダイレクトされますユーザーがアクセスしようとした最後のページにリダイレクトできるように認証方法を変更するにはどうすればよいですか?

+1

は 'セッションです'と 'セッション[ "user_return_toは"]'と同じことになって? –

+0

私は自分のwikiによればそう信じています:https://github.com/plataformatec/devise/wiki/How-To:Redirect-back-to-current-page-after-sign-in,-ign-out,-サインアップ、アップデート。私はまた両方を試みた –

答えて

0

Deviseには、場所を格納するためのbuilt in mechanismがあります。

class ApplicationController < ActionController::Base 
    before_action :store_location!, 
    unless: -> { devise_controller? || request.xhr? }, 
    if: -> { request.get? && is_navigational_format? } 

    before_action :authenticate_user!, 
    unless: :devise_controller? # prevents a circular redirect 


    private 

    def store_location! 
    # from Devise::Controllers::StoreLocation 
    # :user is the scope 
    store_location_for(:user, request.fullpath) 
    end 
end 

:store_location!コールバックは、:authenticate_user!はフィルタチェーンを停止した場所を記憶することができる前にリダイレクトされるように:authenticate_user!の前に配置する必要があります。

if:およびunless:オプションを使用すると、コントローラのコールバックに条件を適用できます。これは、ifunlessのrubyキーワードと同じではありません。

devise_controller?およびis_navigational_format?は、Deviseヘルパーです。 devise_controller?を使用して、特定のパスをブラックリストに登録するのではなく、Devise関連のコントローラでコールバックをスキップする必要があります。デフォルトのナビゲート可能なフォーマットは['*/*', :html]です。

それはすでに保存された場所を使用するようafter_sign_in_path_forをオーバーライドする必要はありません:[:previous_url]

def after_sign_in_path_for(resource_or_scope) 
    stored_location_for(resource_or_scope) || signed_in_root_path(resource_or_scope) 
end 
+0

これのためのwikiは本当に時代遅れです、私はある日更新するべきであると思っているが、人生は途方もない取得し続けます。あなたが望むならば、これをwikiに追加してください。 – max

関連する問題