Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Untitled #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/devise/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/devise/twitter/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/devise/twitter/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Devise
module Twitter
VERSION = "0.1.2.pre"
VERSION = "0.1.2"
end
end
16 changes: 8 additions & 8 deletions lib/devise/twitter/warden.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,17 +23,17 @@
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?

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

Expand Down