pelle / oauth-plugin

Rails plugin for OAuth
http://stakeventures.com/articles/2009/07/21/consuming-oauth-intelligently-in-rails
MIT License
716 stars 216 forks source link

token.user_id won't save, but only in Rails 3.1 for the consumer #96

Open yutai opened 12 years ago

yutai commented 12 years ago

Ok, I know there's already been a few posts on this one already... https://github.com/pelle/oauth-plugin/issues/88 https://github.com/pelle/oauth-plugin/issues/84

I've tried to build the sample provider-consumer from the excellent tutorial http://unhandledexpression.com/2011/06/02/rails-and-oauth-plugin-part-1-the-provider/ http://unhandledexpression.com/2011/06/28/rails-and-oauth-plugin-part-2-the-consumer/

I've followed all the fixes, edits, of that tutorial including the advice to add def current_user=(user) current_user = user end to the consumer's application controller

and it works great when running 3.0.7 on the consumer. The provider works regardless of 3 vs 3.1 When running the consumer in 3.1, the user_id keeps saving as nil when saving the token.

Trying to debug into the gem, i found something maddening:

if i go to the token model of the gem, https://github.com/pelle/oauth-plugin/blob/master/lib/oauth/models/consumers/token.rb

in the def for

find_or_create_from_access_token

before line 51, which is


token.save! if token.new_record? or token.changed?

if i log token.user_id, it outputs the right user id.

but after it saves on line 51, log for token.user_id becomes nil

and then, if i just add two more lines after line 51, where it saved the token,


51 token.save! if token.new_record? or token.changed?
52 token.user_id = user.id
53 token.save

magic. it saves the user_id now in the token.

why in the world did it not save it the first time?

and why does this not happen in 3.0.7?

Thanks for reading this far down my messy post.

octopusinc commented 12 years ago

Could the bang be the culprit? http://m.onkey.org/save-save

octopusinc commented 12 years ago

Here is my debugger log that from a Twitter callback:

CALLBACK
User in request_token: #<User id: 1, email: "bla@me.com", ...>
User in access_token: #<User id: 1, email: "bla@me.com", ...>
  TwitterToken Load (0.3ms)  SELECT "consumer_tokens".* FROM "consumer_tokens" WHERE "consumer_tokens"."type" IN ('TwitterToken') AND "consumer_tokens"."user_id" = 1 AND "consumer_tokens"."token" = '[REDACTED]' LIMIT 1
Token before save: #<TwitterToken id: nil, user_id: 1, type: "TwitterToken", ...>
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
   (0.1ms)  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('') LIMIT 1
  SQL (11.1ms)  INSERT INTO "consumer_tokens" ("created_at", "secret", "token", "type", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?)  [["created_at", Mon, 07 Nov 2011 02:14:25 UTC +00:00], ["secret", "[REDACTED]"], ["token", "[REDACTED]"], ["type", "TwitterToken"], ["updated_at", Mon, 07 Nov 2011 02:14:25 UTC +00:00], ["user_id", nil]]
Token after save: #<TwitterToken id: 25, user_id: nil, type: "TwitterToken", ...>
Token after update: #<TwitterToken id: 25, user_id: 1, type: "TwitterToken", ...>
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
   (0.5ms)  UPDATE "consumer_tokens" SET "user_id" = 1, "updated_at" = '2011-11-07 02:14:25.032692' WHERE "consumer_tokens"."type" IN ('TwitterToken') AND "consumer_tokens"."id" = 25
Redirected to http://...

Something goes wrong during the INSERT that @yutai overwrites with an UPDATE...

octopusinc commented 12 years ago

Solved: Probable Devise method conflict - Rename/replace create_user

#app/models/consumer_token.rb
require 'oauth/models/consumers/token'

class ConsumerToken < ActiveRecord::Base
  include Oauth::Models::Consumers::Token

  # You can safely remove this callback if you don't allow login from any of your services
  before_create :make_user

  # Modify this with class_name etc to match your application
  belongs_to :user

  private

  def make_user
    self.user ||= begin
      User.new {}
      user.save(:validate => false)
    end
  end
end
rtremaine commented 12 years ago

Wow, great find! Been struggling with the same issue for a few hours now. Thanks.