diff --git a/lib/devise/twitter.rb b/lib/devise/twitter.rb index 199d1c4..596037c 100644 --- a/lib/devise/twitter.rb +++ b/lib/devise/twitter.rb @@ -12,15 +12,25 @@ module Twitter mattr_accessor :scope @@scope = nil + + mattr_accessor :twitter_handle_field + @@twitter_handle_field = "twitter_handle" + + mattr_accessor :twitter_oauth_token_field + @@twitter_oauth_token_field = "twitter_oauth_token" + + mattr_accessor :twitter_oauth_secret_field + @@twitter_oauth_secret_field = "twitter_oauth_secret" + # Default way to setup Devise. Run rails generate devise_install to create # a fresh initializer with all configuration values. def self.setup raise "Can not invoke setup twice" if @@setup_done yield self @@setup_done = true - + devise :twitterable_oauth Devise.warden do |manager| - manager.oauth(:twitter) do |twitter| + manager.oauth(:twitterable) do |twitter| twitter.consumer_key = @@consumer_key twitter.consumer_secret = @@consumer_secret twitter.options = { @@ -31,7 +41,7 @@ def self.setup :realm => "http://api.twitter.com/" } end - manager.default_strategies(:scope => @@scope).unshift :twitter_oauth + manager.default_strategies(:scope => @@scope).unshift :twitterable_oauth end end end diff --git a/lib/devise/twitter/rack.rb b/lib/devise/twitter/rack.rb index 6fb755c..328c10f 100644 --- a/lib/devise/twitter/rack.rb +++ b/lib/devise/twitter/rack.rb @@ -12,7 +12,7 @@ module Rack # Perform _only_ the twitter_oauth strategy. # Emulate _perform_authentication in _warden/proxy.rb - strategy = warden.send(:_fetch_strategy, :twitter_oauth, scope) + strategy = warden.send(:_fetch_strategy, :twitterable_oauth, scope) strategy.authenticate! if strategy.user warden.set_user(strategy.user, :event => :authentication, :scope => scope) @@ -21,7 +21,7 @@ module Rack redirect_to Warden::OAuth::Utils.host_with_port(request) else # Perform the redirect to Twitter - strategy = warden.send(:_fetch_strategy, :twitter_oauth, scope) + strategy = warden.send(:_fetch_strategy, :twitterable_oauth, scope) # warden_oauth would always redirect to / so we need to hook into it request_token = strategy.consumer.get_request_token(:oauth_callback => request.url) diff --git a/lib/devise/twitter/version.rb b/lib/devise/twitter/version.rb index f9e81ba..4bbe623 100644 --- a/lib/devise/twitter/version.rb +++ b/lib/devise/twitter/version.rb @@ -1,5 +1,5 @@ module Devise module Twitter - VERSION = "0.1.2.pre" + VERSION = "0.1.2" end end diff --git a/lib/devise/twitter/warden.rb b/lib/devise/twitter/warden.rb index 8f6065e..ebc449f 100644 --- a/lib/devise/twitter/warden.rb +++ b/lib/devise/twitter/warden.rb @@ -5,13 +5,13 @@ if perform_connect # Add twitter_handle to current user - already_existing_user = klass.find_by_twitter_handle(twitter_handle) + already_existing_user = klass.first(Devise::Twitter.twitter_handle_field => twitter_handle) if already_existing_user.blank? # We don't know anyone with this handle, therefore continue with connecting user = @env['warden'].user - user.twitter_handle = twitter_handle - user.twitter_oauth_token = access_token.token - user.twitter_oauth_secret = access_token.secret + user.send("#{Devise::Twitter.twitter_handle_field}=",twitter_handle) + user.send("#{Devise::Twitter.twitter_oauth_token_field}=", access_token.token) + user.send("#{Devise::Twitter.twitter_oauth_secret_field}=", access_token.secret) user.save return user else @@ -23,7 +23,7 @@ previous_user = @env['warden'].user # Try to find user by token - user = klass.find_by_twitter_oauth_token_and_twitter_oauth_secret(access_token.token, access_token.secret) + user = klass.first(:Devise::Twitter.twitter_oauth_token_field=> access_token.token, Devise::Twitter.twitter_oauth_secret_field => access_token.secret) # Since we are logging in a new user we want to make sure the before_logout hook is called @env['warden'].logout if previous_user.present? @@ -31,9 +31,9 @@ if user.nil? # Create user if we don't know him yet user = klass.new - user.twitter_handle = twitter_handle - user.twitter_oauth_token = access_token.token - user.twitter_oauth_secret = access_token.secret + user.send("#{Devise::Twitter.twitter_handle}=",twitter_handle) + user.send("#{Devise::Twitter.twitter_oauth_token}=", access_token.token) + user.send("#{Devise::Twitter.twitter_oauth_secret}=", access_token.secret) user.save end