help adding new strategy

2,662 views
Skip to first unread message

p.

unread,
Jul 28, 2010, 7:30:19 PM7/28/10
to Devise
I'm using Devise 1.0.7 on rails 2.3.5

Very confused as to how to add new strategy. Here's what I've done
thus far.

Created file in my lib directory with strategy:

require 'devise/strategies/base'

module FbAuthenticatable #:nodoc:
module Strategies #:nodoc:
class FbAuthenticatable < ::Devise::Strategies::Base

def valid?
true
end

def authenticate!
u = User.find_by_fb_uid(params[:fb_uid])
if u
success!(u)
else
fail!('Could not match your Facebook ID with any existing
user.')
end
end

end
end
end

Warden::Strategies.add(:fb_authenticatable,
FbAuthenticatable::Strategies::FbAuthenticatable)

I've loaded that through an initializer.

Now, here is where I'm stuck. For various reasons, I have to use my
own controller for signing in (mainly because I have to talk to grab
info from the cookie and then talk to the FB API to grab additional
information). So I just created a new controller and copy and pasted
the sessions code for the create method in the sessions controller.

class FbController < ApplicationController
before_filter :current_facebook_user

def sign_in
current_facebook_user.fetch
params[:fb_uid] = current_facebook_user.id
resource = warden.authenticate!(:scope => resource_name, :recall
=> "new")
set_flash_message :notice, :signed_in
sign_in_and_redirect(resource_name, resource)
end

end

So, my questions are:
1. Is this the right tact to take so far?
2. If so, how do I set the resource name (I'm having a hard time
getting my head around scoping and what and why to set things in the
devise config)?

I can't help but think there's a more elegant way of doing this, but I
can't seem to figure out how...

p.

unread,
Jul 29, 2010, 12:45:30 PM7/29/10
to Devise
Sorted out a solution to this. Added a field to the sessions new form
for storing additional information, populated it through an ajax call
and then just used the regular sessions controller. 2 trips to the
server, but i didn't have to muck with modifying the controller.

At any rate, a basic example like the following on the Wiki would have
helped immensely so people don't have to go digging through code in
order to understand the working of the warden/devise strategies:

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



One question: With what i've outlined above, it occurs to me I don't
have have control over which initializers are loaded first, which
won't be a problem if devise does lazy loading. does it? if not, where
is a more appropriate place to put the strategy?

PanosJee

unread,
Sep 7, 2010, 10:59:59 AM9/7/10
to Devise
Hi p. I am trying your solution but once I get to
http://localhost:3000/users/sign_in?warden_provider=twitter
it does not redirect it jsut displays the login form
The route I use is user_session_path(:warden_provider => :twitter)

On Jul 29, 7:45 pm, "p." <ppetr...@gmail.com> wrote:
> Sorted out a solution to this. Added a field to the sessions new form
> for storing additional information, populated it through an ajax call
> and then just used the regular sessions controller. 2 trips to the
> server, but i didn't have to muck with modifying the controller.
>
> At any rate, a basic example like the following on the Wiki would have
> helped immensely so people don't have to go digging through code in
> order to understand the working of thewarden/devisestrategies:
>
> 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.wardendo |manager|
>      manager.default_strategies.unshift :custom_strategy_name
>   end
>
> One question: With what i've outlined above, it occurs to me I don't
> have have control over which initializers are loaded first, which
> won't be a problem ifdevisedoes lazy loading. does it? if not, where
> is a more appropriate place to put the strategy?
>
> On Jul 28, 4:30 pm, "p." <ppetr...@gmail.com> wrote:
>
>
>
> > I'm usingDevise1.0.7 on rails 2.3.5
>
> > Very confused as to how to add new strategy. Here's what I've done
> > thus far.
>
> > Created file in my lib directory with strategy:
>
> > require 'devise/strategies/base'
>
> > module FbAuthenticatable #:nodoc:
> >   moduleStrategies#:nodoc:
> >     resource =warden.authenticate!(:scope => resource_name, :recall
Reply all
Reply to author
Forward
0 new messages