2012-04-26 3 views
0

私はdeviseとomniauthを使用して、ユーザーが自分のTwitterアカウントやローカルログイン資格情報で登録/ログインできるようにしています。すべての登録とログインのためにうまく動作します。私の問題は、ユーザーが最初にローカルパスワードを設定せずに自分のTwitter認証を破棄することを選択したときに発生します。ユーザーが権限を壊した場合、それらをnew_password_pathにルーティングして、今後のログイン用のパスワードを選択できるようにしたいと考えています。ここでdevise/omniauthで承認を削除するときにユーザーがパスワードを入力できるようにする方法

は、コントローラのコードです:

class AuthenticationsController < ApplicationController 
     before_filter :authenticate_user!, :except => [:create, :failure] 

     def create 
     omniauth = request.env["omniauth.auth"] 
     authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid']) 
     if authentication #existing user is logging-in with existing authentication service 
      flash[:notice] = "Signed in successfully." 
      set_home_location_cookies(authentication.user, authentication.user.home_lat, authentication.user.home_lng) 
      sign_in(:user, authentication.user) 
      redirect_to root_path 
     elsif current_user #existing user who is already logged-in is creating a new authentication service for future use 
      current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'], :token => omniauth['credentials']['token']) 
      current_user.update_posting_preferences(omniauth['provider']) 
      flash[:notice] = "Successfully linked to your #{omniauth['provider'].titleize} account." 
      redirect_to root_path 
     else #new user is creating a new authentication service and logging in 
      user = User.new 
      user.apply_omniauth(omniauth) 
      if user.save 
      flash[:notice] = "Signed in successfully." 
      sign_in(:user, user) 
      redirect_to root_path 
      else 
      session[:omniauth] = omniauth.except('extra') 
      session[:user_message] = {:success => false, :message => "userSaveError"} 
      redirect_to new_user_registration_url 
     end 
     end 
     end 


     def failure 
     flash[:alert] = "Could not authorize you from your social service." 
     redirect_to root_path 
     end 

     def destroy 
     @authentication = current_user.authentications.find(params[:id]) 
     current_user.update_posting_preferences(@authentication.provider) 
     @authentication.destroy 
     flash[:notice] = "You have successfully destroyed your link to your #{@authentication.provider.titleize} account." 
     if current_user.authentications.empty? && current_user.encrypted_password.empty? 
      sign_out 
      flash[:alert] = "Alert: Your account does not currently have a password for account authorization. You are in danger of losing your account unless you create a new password by using this form." 
      redirect_to new_password_path(current_user) and return 
     else 
      redirect_back_or(root_path) 
     end 
     end 

redirect_to new_password_path(current_user) and returnコマンド enter image description here

によってトリガ「ゼロの有効なマッピングを見つけることができませんでした」エラーのコードの結果、私は非常に考え出すいくつかの助けをいただければ幸いですこの問題を解決する。

ありがとうございます!

答えて

1

OK。私はそれを認めます。私はチュートリアルから認証制御器を実装しましたが、何が舞台裏で起こっているのかを知るためにルーティングを検討することはありませんでした。昨夜、私はドキュメントを見直し、私の問題を理解しました。面白いことは、上記のルーチンが古いバージョンのdeviseで動作しましたが、1.5.3で動作しないということです。

destroyアクションでは、current_userをサインアウトして、パラメータとしてcurrent_userを送信するnew_password_pathにルーティングしようとします。驚くことではないが、その時点で「current_user」がゼロになっている。だから、私は、 "nilのための有効なマッピングを見つけることができませんでした"エラーが発生します。ここに私の簡単な修正があります:

def destroy 
    @authentication = current_user.authentications.find(params[:id]) 
    user = current_user 
    current_user.update_posting_preferences(@authentication.provider) 
    @authentication.destroy 
    flash[:notice] = "You have successfully destroyed your link to your #{@authentication.provider.titleize} account." 
    if current_user.authentications.empty? && current_user.encrypted_password.empty? 
     sign_out 
     flash[:alert] = "Alert: Your account does not currently have a password for account authorization. You are in danger of losing your account unless you create a new password by using this form." 
     redirect_to new_password_path(user) and return 
    else 
     redirect_back_or(root_path) 
    end 
    end 
関連する問題