2012-03-19 13 views
0

authenticate_userフィルタをカスタマイズして、認証トークンを介してユーザーを認証することができるようにします。組み込みのトークンメカニズムを使用したくない理由は、それが単一の認証トークンのみを許可するからです。私はユーザーに複数のトークンを持たせたい。私が知っている限り、これをサポートしていません。Devise:authenticate_userフィルタをカスタマイズして認証トークンを確認する

アイデア?

答えて

0

私はこの問題を解決するために自分の戦略を追加しました:

Warden::Strategies.add(:tokens_authenticatable) do 
    def valid? 
    # code here to check whether to try and authenticate using this strategy; 
    return params.key? :token 
    end 

    def authenticate! 
    # code here for doing authentication; 
    puts params 

    token = Token.where(["token = ?", params[:token]]).first 
    puts token 
    if (!token.nil?)  
     # if successful, call 
     success!(token.user) # where resource is the whatever you've authenticated, e.g. user; 
    else 
     # if fail, call 
     fail!("WWAAAAA") # where message is the failure message 
    end 
    end 
end 

config.warden do |manager| 
    # manager.intercept_401 = false 
    manager.default_strategies(:scope => :user).unshift :tokens_authenticatable 
end 
関連する問題