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

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

Open
yutai opened this issue Nov 6, 2011 · 4 comments
Open

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

yutai opened this issue Nov 6, 2011 · 4 comments

Comments

@yutai
Copy link

yutai commented Nov 6, 2011

Ok, I know there's already been a few posts on this one already...
#88
#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
Copy link

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

@octopusinc
Copy link

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", ...>
  �[1m�[36mTwitterToken Load (0.3ms)�[0m  �[1mSELECT "consumer_tokens".* FROM "consumer_tokens" WHERE "consumer_tokens"."type" IN ('TwitterToken') AND "consumer_tokens"."user_id" = 1 AND "consumer_tokens"."token" = '[REDACTED]' LIMIT 1�[0m
Token before save: #<TwitterToken id: nil, user_id: 1, type: "TwitterToken", ...>
  �[1m�[35mCACHE (0.0ms)�[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  �[1m�[35m (0.1ms)�[0m  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('') LIMIT 1
  �[1m�[36mSQL (11.1ms)�[0m  �[1mINSERT INTO "consumer_tokens" ("created_at", "secret", "token", "type", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?)�[0m  [["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", ...>
  �[1m�[35mUser Load (0.3ms)�[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  �[1m�[36m (0.5ms)�[0m  �[1mUPDATE "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�[0m
Redirected to http://...

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

@octopusinc
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants