2012-04-26 80 views
0

我有一個rais 3應用程序,它使用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

好的。我承認這一點。我從教程中實施了身份驗證控制器,而無需研究設計路由,以瞭解幕後操作。昨天晚上我回顧了文檔並找出了我的問題。有趣的是,上面的例程在舊版本的設計上工作,但在設計1.5.3上不起作用。

在destroy操作中,我註銷current_user,然後嘗試路由到以「current_user」作爲參數發送的new_password_path。毫不奇怪,在那個時候「current_user」已被清除。因此,我得到了「無法找到有效映射」的錯誤。這裏是我的簡單修復:

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