2013-09-21 99 views
1

所以,我嘗試使用的認證令牌來直接登錄鏈接,但我發現在我的服務器日誌Filter chain halted as :after_token_authentication rendered or redirected直接鏈接登錄停止

class ApplicationController < ActionController::Base 
     protect_from_forgery 
     before_filter :store_location 
     before_filter :authenticate_user! 
     before_filter :after_token_authentication 
     check_authorization :unless => :devise_controller? 

     rescue_from CanCan::AccessDenied do |exception| 
     session[:previous_url] = nil 
     redirect_to root_url 
     end 


     def store_location 
     # reset_session 
     # store last url - this is needed for post-login redirect to whatever the user last visited. 

     if (request.fullpath != "/user/sign_in" && \ 
      request.fullpath != "/user/sign_up" && \ 
      request.fullpath != "/user/password" && \ 
      !request.fullpath.include?("/user") && \ 
      !request.fullpath.include?("/admin") && \ 
      !request.xhr?) # don't store ajax calls 
      session[:previous_url] = request.fullpath 
     end 
     end 

     def after_sign_in_path_for(resource) 
     if current_user.admin? 
      admin_dashboard_path 
     elsif current_user.contractor? 
      if current_user.contractor.business_name == 'Test Devise' 
      'url' 
      else 
      contractor_dashboard_path 
      end 
     else 
      session[:previous_url] || homeowner_service_requests_path 
     end 
     end 

     #generates 6-digit random password (a-z, 0-9) for sending to users when BuildZoom creates user account after they express interest in a service request, leave a review 
     def generate_temporary_password 
     rand(36**6).to_s(36) 
     end 




     protected 


     def after_token_authentication 
    if params[:authentication_key].present? 
     @user = User.find_by_authentication_token(params[:authentication_key]) 
     sign_in @user if @user 
     welcome = @user.approved.eql?(7) and @user.type.eql?(2) 
     @user.approved = 0 
     @user.save 
     unless welcome 
     redirect_to root_path 
     else 
     redirect_to contractor_welcome_path 
     end 
    elsif params[:auth_token].present? && params[:location] == "galleries" 
     @current_user = User.find_by_authentication_token(params[:auth_token]) 
     sign_in @current_user if @current_user 
     @current_user.approved = 0 
     @current_user.save 
     debugger 
     redirect_to contractor_list_galleries_path 
    end 
    end 

     def after_sign_out_path_for(resource) 
     'url' 
     end 
    end 

這裏是服務器日誌:

Started GET "/?auth_token=uN8QFMsocpDyhWKCx9QN&location=galleries" for 127.0.0.1 at 2013-09-20 22:38:37 -0700 
Processing by PagesController#home as HTML 
    Parameters: {"auth_token"=>"uN8QFMsocpDyhWKCx9QN", "location"=>"galleries"} 
Geokit is using the domain: 
    User Load (189.2ms) SELECT `users`.* FROM `users` WHERE `users`.`authentication_token` = 'uN8QFMsocpDyhWKCx9QN' LIMIT 1 
    (109.5ms) BEGIN 
    (111.7ms) UPDATE `users` SET `last_sign_in_at` = '2013-09-21 05:38:28', `current_sign_in_at` = '2013-09-21 05:38:38', `sign_in_count` = 61, `updated_at` = '2013-09-21 05:38:38' WHERE `users`.`uid` = 149407 
    (95.6ms) COMMIT 
    User Load (184.3ms) SELECT `users`.* FROM `users` WHERE `users`.`authentication_token` = 'uN8QFMsocpDyhWKCx9QN' LIMIT 1 
    (93.3ms) BEGIN 
    (109.5ms) COMMIT 
Redirected to http://0.0.0.0:3000/contractor/galleries 
Filter chain halted as :after_token_authentication rendered or redirected 
Completed 302 Found in 10884ms (ActiveRecord: 893.2ms) 


Started GET "/contractor/galleries" for 127.0.0.1 at 2013-09-20 22:38:48 -0700 
Processing by Contractor::ContractorGalleriesController#index as HTML 
Geokit is using the domain: 
Completed 401 Unauthorized in 1ms 


Started GET "/contractor/galleries" for 127.0.0.1 at 2013-09-20 22:38:49 -0700 
Processing by Contractor::ContractorGalleriesController#index as HTML 
Geokit is using the domain: 
Completed 401 Unauthorized in 1ms 


Started GET "/user/sign_in" for 127.0.0.1 at 2013-09-20 22:38:49 -0700 
Processing by Devise::SessionsController#new as HTML 
Geokit is using the domain: 
    Rendered devise/shared/_links.erb (0.5ms) 
    Rendered devise/sessions/new.html.erb within layouts/application (3.7ms) 
    Rendered shared/_mixpanel.html.erb (0.2ms) 
    Rendered layouts/_head.html.erb (12.4ms) 
    Rendered layouts/_navigation.html.erb (0.6ms) 
    Rendered layouts/_messages.html.erb (0.1ms) 
    Rendered shared/_olark (0.0ms) 
    Rendered layouts/_footer.html.erb (1.2ms) 
Completed 200 OK in 64ms (Views: 62.5ms | ActiveRecord: 0.0ms) 


Started GET "/user/sign_in" for 127.0.0.1 at 2013-09-20 22:38:49 -0700 
Processing by Devise::SessionsController#new as HTML 
Geokit is using the domain: 
    Rendered devise/shared/_links.erb (0.5ms) 
    Rendered devise/sessions/new.html.erb within layouts/application (4.8ms) 
    Rendered shared/_mixpanel.html.erb (0.1ms) 
    Rendered layouts/_head.html.erb (11.5ms) 
    Rendered layouts/_navigation.html.erb (0.6ms) 
    Rendered layouts/_messages.html.erb (0.0ms) 
    Rendered shared/_olark (0.0ms) 
    Rendered layouts/_footer.html.erb (1.1ms) 
Completed 200 OK in 113ms (Views: 111.4ms | ActiveRecord: 0.0ms) 
+0

能否請您檢查@ current_user.save將返回true或false值,如果是假的,然後檢查爲什麼它返回false。這應該是真的,那麼它會起作用。 –

回答

1

將其從@user更改爲@current_userafter_token_authentication。你authenticate_user!調用需要@current_user被設置爲每點擊這裏:where is devise implementation of "authenticate_user!" method?

+0

我認爲它的工作,但nope – bl0b

+1

所以我不得不補充:'sign_in @current_user,:bypass => true' http://rubydoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers#sign_in-instance_method – bl0b

0

此消息僅表示在您之前的某個過濾器中呈現了模板或發生了重定向。在你的情況下,after_token_authentication過濾器重定向到contractor_list_galleries_path,這結束了過濾器之前的鏈。此消息不是錯誤,只是顯示告訴你重定向發生在過濾器之前而不是實際的行爲。

+0

但那麼爲什麼用戶被重定向到登錄屏幕? 我確信在重定向之前用戶已經過身份驗證,但他仍然得到401未授權 – bl0b

+0

重定向發生在'contractor_list_galleries_path'頁面的重定向之後,所以我無法確定發生了什麼。也許批准的屬性沒有被設置,因爲我沒有看到'@ user.save'行發生另一個更新sql查詢。 – jvperrin

+0

已批准已處於0.因此,它不會更新它。 但我不明白爲什麼在重定向用戶不必去圖庫的權限,即使我登錄他,然後第二次它是after_token_authentication方法,他沒有登錄了 – bl0b