Created
April 6, 2011 19:33
-
-
Save r00k/906356 to your computer and use it in GitHub Desktop.
Custom devise strategy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In config/initializers/local_override.rb: | |
require 'devise/strategies/authenticatable' | |
module Devise | |
module Strategies | |
class LocalOverride < Authenticatable | |
def valid? | |
true | |
end | |
def authenticate! | |
if params[:user] | |
user = User.find_by_email(params[:user][:email]) | |
if user && user.encrypted_password == params[:user][:password] | |
success!(user) | |
else | |
fail | |
end | |
else | |
fail | |
end | |
end | |
end | |
end | |
end | |
Warden::Strategies.add(:local_override, Devise::Strategies::LocalOverride) | |
# In config/initializers/devise.rb | |
config.warden do |manager| | |
manager.default_strategies(:scope => :user).unshift :local_override |
Sorry, it's been a while since I've looked at this. I'm afraid the answer to that question is left as an exercise to the reader :)
I forked it and put my spec in there for hungry minds.
Thanks!
thanks for code as well specs :)
Thanks! This was really helpful but to get this working on my end, I had to replace
if user && user.encrypted_password == params[:user][:password]
with
if user && user.valid_password?(params[:user][:password])
I also had to throw a
fail!
in order to get fail to also halt the authentication process.
Really helpful thanks!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How might one test this using rspec?