public
Last active

Custom devise strategy

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# 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

How might one test this using rspec?

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.

https://gist.github.com/1367606

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.