Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom authentication strategy for devise

I need to write a custom authentication strategy for https://github.com/plataformatec/devise but there doesn't seem to be any docs. How's it done?

like image 607
opsb Avatar asked Nov 19 '10 07:11

opsb


People also ask

What is an authentication strategy?

AuthenticationStrategy is a standard interface that the @loopback/authentication package understands. Hence, any authentication strategy that adopts this interface can be used with @loopback/authentication . Think of it like the standard interface for Passport.

What is devise authentication?

Devise is an excellent authentication system made for Rails that allows us to easily drop-in User functionality into our project. Add Devise to your Gemfile and run bundle install. gem 'devise', '~> 3.4.0'


1 Answers

I found this very helpful snippet in this thread on the devise google group

initializers/some_initializer.rb:

Warden::Strategies.add(:custom_strategy_name) do    def valid?      # code here to check whether to try and authenticate using this strategy;      return true/false    end     def authenticate!      # code here for doing authentication;      # if successful, call       success!(resource) # where resource is the whatever you've authenticated, e.g. user;     # if fail, call      fail!(message) # where message is the failure message    end  end  

add following to initializers/devise.rb

  config.warden do |manager|       manager.default_strategies.unshift :custom_strategy_name    end  
like image 184
opsb Avatar answered Sep 20 '22 06:09

opsb