Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom devise strategy
# 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
@jancel

This comment has been minimized.

Copy link

@jancel jancel commented Nov 7, 2011

How might one test this using rspec?

@r00k

This comment has been minimized.

Copy link
Owner Author

@r00k r00k commented Nov 15, 2011

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 :)

@jancel

This comment has been minimized.

Copy link

@jancel jancel commented Nov 15, 2011

I forked it and put my spec in there for hungry minds.

https://gist.github.com/1367606

@r00k

This comment has been minimized.

Copy link
Owner Author

@r00k r00k commented Nov 15, 2011

Thanks!

@aag1091

This comment has been minimized.

Copy link

@aag1091 aag1091 commented Apr 17, 2013

thanks for code as well specs :)

@michaeljkchoi

This comment has been minimized.

Copy link

@michaeljkchoi michaeljkchoi commented Nov 13, 2013

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.

@ALFmachine

This comment has been minimized.

Copy link

@ALFmachine ALFmachine commented Sep 20, 2016

Really helpful thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.