18

Warden/Devise を使用して、ログインした (またはログインに失敗した) ページにユーザーをリダイレクトする方法を見つけようとしています。利用可能な、または利用可能になる可能性のあるセッション変数がどこかにあると思います。

例 シナリオ 1: 許可されていないユーザーが保護されたページ X にアクセスします。ログインページにリダイレクトされました。ユーザーがログインします。ユーザーは保護されたページ x にリダイレクトされました

シナリオ 2: 許可されていないユーザーが、ページ x で保護されたアクションを実行したいと考えています。ユーザーがログイン リンクをクリックします。ユーザーがログインします。ユーザーがページ x にリダイレクトされ、アクションが利用できるようになりました

任意のポインタをいただければ幸いです。

ありがとう!

4

5 に答える 5

16

after_sign_in_path_for(resource)http://rdoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers )と呼ばれるデバイスヘルパーメソッドとsession[:"user.return_to"]、最後のURLを格納すると呼ばれるセッション変数があります。メソッドはafter_sign_in_path_for文字列を返す必要があり、デバイスはログイン後にこのパスを自動的に使用してユーザーをリダイレクトします。

私のアプリケーションコントローラーには、セッション変数が設定されていない場合にユーザーをホームページにリダイレクトする次のものを配置しました。

def after_sign_in_path_for(resource)
    (session[:"user.return_to"].nil?) ? "/" : session[:"user.return_to"].to_s
end
于 2010-10-13T07:17:55.507 に答える
1

承認にCanCanを使用している場合は、以下を追加することでこれを実現できます。そうでない場合は、概念を現在の認証システムに適合させることができるはずです。

app / controllers / application_controller.rb

  rescue_from CanCan::AccessDenied do |exception|
    flash[:error] = exception.message
    if user_signed_in?
      redirect_to root_url
    else
      # Adds the protected page to the login url but only if the user is not logged in
      redirect_to login_path(:next => request.path)
    end
  end

  def after_sign_in_path_for(resource_or_scope)
    # if a protected page found, then override the devise after login path
    params[:user]["next"] || super
  end

app / views / devise / sessions / new.html.erb

  <% if params[:next] %>
      <%= f.hidden_field :next, :value => params[:next] %>
  <% end %>

このソリューションでは、セッション変数を使用する代わりに、URLのパラメーターを使用して保護されたページを追跡します。

于 2010-12-03T17:50:51.683 に答える
1

request.referer前の を取得するために使用できますURL

于 2010-09-19T14:21:38.140 に答える
1

devise (3.5.2)うわー、これはすべて舞台裏 (Devise::SessionsController#newアクションの周り) で自動的に行われることに気付きました。追加のコントローラーの変更は必要ありません。

store明示的に/ getpreviousする必要がある場合locationは、以前の回答を参照してください。

現在(2015年秋)​​、よりセクシーな方法があります:

Devise::Controllers::StoreLocation#store_location_for:

# Stores the provided location to redirect the user after signing in.
# Useful in combination with the `stored_location_for` helper.

store_location_for :user, dashboard_path
redirect_to user_omniauth_authorize_path :facebook

Devise::Controllers::StoreLocation#stored_location_for:

# Returns and delete (if it's navigational format) the url stored in the session for
# the given scope. Useful for giving redirect backs after sign up:

redirect_to stored_location_for(:user) || root_path

メソッドは、読み取り後に関連するsessionキーと値の削除を処理します。必要なのは、:resourceキー (:user上記の例) と保存するパス (上記の例)を提供することだけですdashboard_path。詳細はソース参照。

実際の答えについては、次のようになります。

class ApplicationController < ActionController::Base
  rescue_from CanCan::AccessDenied, with: :access_denied

  # ...

  private

  def access_denied(exception)
    store_location_for :user, request.path
    redirect_to user_signed_in? ? root_path : new_user_session_path, alert: exception.message
  end

  def after_sign_in_path_for(resource)
    stored_location_for(:user) || root_path
  end
end
于 2015-09-29T20:27:15.083 に答える
0

ここに私が思いつくことができる最高のものがあります。Facebook認証でも完璧に動作します。セッション変数への URL の先頭に追加する制限を追加することで、ユーザーに返してほしくないパスをどんどん削除できます (例: コールバック、スプラッシュ ページ、ランディング ページなど)。

#ApplicationsController

after_filter :store_location

def store_location
  session[:previous_urls] ||= []
  # store unique urls only
  session[:previous_urls].prepend request.fullpath if session[:previous_urls].first != request.fullpath && request.fullpath != "/user" && request.fullpath != "/user/login" && request.fullpath != "/" && request.fullpath != "/user/logout" && request.fullpath != "/user/join" && request.fullpath != "/user/auth/facebook/callback"
  # For Rails < 3.2
  # session[:previous_urls].unshift request.fullpath if session[:previous_urls].first != request.fullpath 
  session[:previous_urls].pop if session[:previous_urls].count > 3
end

def after_sign_in_path_for(resource) 
  @url = session[:previous_urls].reverse.first
  if @url != nil
    "http://www.google.com" + @url
  else
    root_path
  end
end
于 2012-11-13T16:55:36.593 に答える