我在 Rails 3.2 和 Ruby 2.0 中使用 'omniauth' 和 'omniauth-facebook' gem,今天我遇到了一个尴尬的情况,我找不到任何解决方案。在此之前,每个用户都在完美地使用 facebook 登录。
我的路线
# Facebook authentication
match 'auth/:provider/callback' => 'sessions#create'
我的会话控制器
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.uid
redirect_to user
end
....
我的会话模型
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.id = auth.uid
user.uid = auth.uid
user.name = auth.info.name
if !user.username
user.username = auth.uid
end
user.username = auth.info.nickname
user.email = auth.info.email
user.gender = auth.extra.raw_info.gender
user.is_admin = false
user.picture = "http://graph.facebook.com/#{auth.uid}/picture?type=large&height=324&width=580"
user.updated_at = auth.extra.raw_info.updated_time
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
让我告诉你尴尬的部分。一个用户通过了身份验证,我们称它为 TESTUSER,但不是他当前的 id,Rails 重定向并设置不同于其原始 ID 的 Session id。
User Load (132.4ms) SELECT `users`.* FROM `users` WHERE `users`.`provider` = 'facebook' AND `users`.`uid` = 100006300277396 LIMIT 1
(129.6ms) BEGIN
SQL (130.6ms) INSERT INTO `users` (`age`, `bio`, `created_at`, `email`, `experience`, `fat_percentage`, `gender`, `height`, `id`, `is_admin`, `location`, `mass`, `name`, `oauth_expires_at`, `oauth_token`, `password`, `picture`, `provider`, `salt`, `uid`, `updated_at`, `username`) VALUES (NULL, NULL, '2013-07-09 15:32:14', 'TESTUSER@yandex.com', NULL, NULL, 'male', NULL, 100006300277396, 0, NULL, NULL, 'Test User', '2013-09-07 06:49:53', 'CAAHrxXWCZBwsBAEtkOVHkTHC5bMPlr6Q9IbWRSCBLzfEZBmSwKZAtr9ebHCxjO4xStcg9tew90u9fhVl2cTcIx7za6nJYz4wuMQmx7UrQDQntT1nGVOFy87ctuDAHS1jQueV0KXC3MvkWGZCn9ccprzJ8w4pKP0ZD', NULL, 'http://graph.facebook.com/100006300277396/picture?type=large&height=324&width=580', 'facebook', NULL, 100006300277396, '2013-07-09 10:33:49', 'TESTUSER')
Started GET "/users/100006300277396" for 88.248.139.159 at 2013-07-09 15:32:14 +0000
Processing by UsersController#show as HTML
Parameters: {"id"=>"100006300277396"}
User Load (130.8ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 100006300277396 LIMIT 1
Completed 404 Not Found in 170ms
ActiveRecord::RecordNotFound (Couldn't find User with id=100006300277396):
app/controllers/users_controller.rb:17:in `show'
在控制台中它说用户的 ID 是 100006300277396,但在数据库上它设置为 2147483647。
我发现 MYSQL 语句工作得很好,但是因为 ID 之前是自动递增的,所以它将最后一个自动递增的值插入到数据库中。因此,问题出在数据库上,您是否看到任何错误?