NoamB/sorcery — Issue #224: Failed to login from twitter
Author: burlesona
Created: 2012-02-27 03:15:39+00:00
State: open

I followed the External wiki tutorial exactly, and no matter what I do I cannot get login from twitter to work.

```
Started GET "/oauth/callback?provider=twitter&oauth_token=GiWM1yuYsyLCwPBroovkMk3fYejTBZmhWF5SHAFF0&oauth_verifier=ztV6u9wybODD2WrBCQ2AeRCGPumdfqVxDFGUXbpbu58" for 127.0.0.1 at 2012-02-26 21:10:14 -0600
Processing by OauthsController#callback as HTML
Parameters: {"provider"=>"twitter", "oauth_token"=>"GiWM1yuYsyLCwPBroovkMk3fYejTBZmhWF5SHAFF0", "oauth_verifier"=>"ztV6u9wybODD2WrBCQ2AeRCGPumdfqVxDFGUXbpbu58"}
Authentication Load (0.3ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."provider" = 'twitter' AND "authentications"."uid" = '15588072' LIMIT 1
(0.1ms) begin transaction
SQL (54.6ms) INSERT INTO "users" ("created_at", "crypted_password", "email", "remember_me_token", "remember_me_token_expires_at", "reset_password_email_sent_at", "reset_password_token", "reset_password_token_expires_at", "salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Mon, 27 Feb 2012 03:10:17 UTC +00:00], ["crypted_password", nil], ["email", nil], ["remember_me_token", nil], ["remember_me_token_expires_at", nil], ["reset_password_email_sent_at", nil], ["reset_password_token", nil], ["reset_password_token_expires_at", nil], ["salt", nil], ["updated_at", Mon, 27 Feb 2012 03:10:17 UTC +00:00], ["username", "burlesona"]]
(1.7ms) rollback transaction
Redirected to http://0.0.0.0:3000/
Completed 302 Found in 3517ms (ActiveRecord: 57.9ms)
```

The request goes to Twitter without a problem, I can login at twitter without a problem, and the callback is triggered without a problem. But for some reason every time the callback is triggered the transaction is rolled back and the user is not created or logged in.

I'm using the OauthsController straight from the tutorial, except I am not using the application-wide "require login" so I do not need to skip that before filter.

```
class OauthsController < ApplicationController
# sends the user on a trip to the provider,
# and after authorizing there back to the callback url.
def oauth
login_at(params[:provider])
end

def callback
provider = params[:provider]
if @user = login_from(provider)
redirect_to root_path, :notice => "Logged in from #{provider.titleize}!"
else
begin
@user = create_from(provider)
# NOTE: this is the place to add '@user.activate!' if you are using user_activation submodule

reset_session # protect from session fixation attack
auto_login @user
redirect_to root_path, :notice => "Logged in from #{provider.titleize}!"
rescue
redirect_to root_path, :alert => "Failed to login from #{provider.titleize}!"
end
end
end
end
```

Here's my user model:

```
class User < ActiveRecord::Base
authenticates_with_sorcery! do |config|
config.authentications_class = Authentication
end

attr_accessible :username, :email, :password, :password_confirmation, :authentications_attributes

has_many :authentications, :dependent => :destroy
accepts_nested_attributes_for :authentications

validates_length_of :password, :minimum => 3, :message => "password must be at least 3 characters long", :if => :password
validates_confirmation_of :password, :message => "should match confirmation", :if => :password
end
```

At this point my mind is blown by this issue, I simply cannot figure out why the user is not being successfully written to the db. I would very much appreciate any help!