3

I have a RoR application that's using the RESTful Authentication plug-in. Everything works great. I recently enabled cookie based authentication and that works fine too. The problem is that I want to change the default landing page when the user is authenticated using a cookie. I want to have a cookie authenticated user redirected to the same page they are redirected to upon successful login from the login form. They are always directed to the original request URL. I'm racking my brain on this as I thought I understood how it works and every change I make seems to have no impact.

I suspect this is something simple but I'm obviously missing it. I'd appreciate any feedback, guidance or suggestions you might offer.

4

5 に答える 5

1

私は問題を解決しましたが、私の意見では少し醜いです。これが私がしたことです。

Cookie 認証メソッドでは、Cookie ログイン メソッドが使用されたことを示すセッション変数を設定しました。

def login_from_cookie
  user = cookies[:auth_token] && User.find_by_remember_token(cookies[:auth_token])
  if user && user.remember_token?
    session[:cookie_login] = true   **# this is my addition**
    self.current_user = user
    handle_remember_cookie! false # freshen cookie token (keeping date)
    self.current_user
  end
end

次に、before_filter set_current_user変数を確認し、変数が設定されている場合はリダイレクトして、変数をnilに設定します。

def set_current_user
  Authorization.current_user = current_user
  if session[:cookie_login] 
    redirect_to :controller => :users, :action => :search
    session[:cookie_login] = false
  end
end

きれいではありませんが、機能します。これをクリーンアップする方法についての提案を歓迎します。

于 2009-05-24T17:44:11.620 に答える
0

そのようにルートを設定することはできませんか

map.root :controller => :users, :action => :search

そして、何らかの「ログイン済み」パラメーターが設定されていることを確認する before_filter がありますか? このパラメーターは、ユーザーがログインするたびに、Cookie または通常の方法で設定する必要があります。その後、Cookie認証が行われるか、通常の認証が行われるかにかかわらず、デフォルトのページに移動します。多分私は問題を誤解しています。

于 2009-11-20T13:26:38.267 に答える
0

ログインに成功したら、次の行をセッション コントローラーに追加できます。

redirect_to :controller => 'dashboard', :action => 'index'
于 2009-05-24T15:30:36.823 に答える
0

私は Bort を使用しているので、これはRestful_Authenticationそれ自体の一部ではないかもしれませんsuccessful_loginが、セッション コントローラーには、この restful_auth メソッドを使用するメソッドがあります。

redirect_back_or_default( root_path )

で定義されているauthenticated_system.rb

    def redirect_back_or_default(default)
      redirect_to(session[:return_to] || default)
      session[:return_to] = nil
    end
于 2009-05-24T16:31:14.457 に答える
0

Restful Authentication は、リクエストが行われたときにアクセスしようとしていた元の URL を保存します。その値を保存しないようにするか、Cookie 認証が実行されたときにその値をクリアすると、ユーザーはデフォルトのページにリダイレクトされます。

私はおそらく authenticated_system.rb でこのようにします

 def login_from_cookie
  user = cookies[:auth_token] && User.find_by_remember_token(cookies[:auth_token])
  if user && user.remember_token?
    self.current_user = user
    session[:return_to] = nil # This clears out the return value so the user will get redirected to the default path
    handle_remember_cookie! false # freshen cookie token (keeping date)
    self.current_user
  end
end

session[:return_to] = nil です

次に、セッションコントローラーでデフォルトパスを設定したことを確認してください。これですべて設定されます。セッション コントローラーのコードは次のようになります。

redirect_back_or_default(the_path_you_want_to_send_them_to)
于 2009-11-25T21:05:23.557 に答える