Authlogic无法找到用户

时间:2010-08-30 15:18:09

标签: ruby-on-rails ruby authentication sqlite authlogic

我正在将facebook connect与我的应用程序集成,后者使用authlogic,facebooker和authlogic_facebook_connect插件。在我登录Facebook后,我被重定向到网站的首页(根据我的代码) - 但页面永远不会加载! - 它挂了。 查看开发日志,我发现正在不断尝试从数据库加载用户。

它一直在继续,每次我重新加载development.log(当页面仍在尝试加载时)它变得更大 - 开发日志在底部。

这是我得到的信息:

  1. 进行一些调试,我将问题追溯到函数意味着使用authlogic来保持用户会话(没有参数):

    UserSession.find

  2. 看起来Authlogic正在尝试使用某个persistence_token在数据库中查找用户。通过控制台直接查看该记录 - 它的持久性标记为空(这可能只是因为正常运行的authlogic将要做的下一件事是设置persistence_token - 尽管不确定)。

  3. 有人可以给我任何指示吗?

    谢谢!


    [4;36;1mUser Load (0.7ms)[0m   [0;1mSELECT * FROM "users" WHERE ("users"."id" = '4') LIMIT 1[0m
    [4;35;1mUser Load (0.6ms)[0m   [0mSELECT * FROM "users" WHERE ("users"."facebook_uid" = 100001121293021) LIMIT 1[0m
    [4;36;1mUser Load (0.2ms)[0m   [0;1mSELECT "users".id FROM "users" WHERE ("users"."persistence_token" = 'ddd6b0d160321e55266db847d0b4558e9ed76ab220ef3fce655c24c5d24286d56d728deda76b44741121b0b78e1c266537fbfe00bf01206a393090c5c1f475' AND "users".id <> 4) LIMIT 1[0m
    [4;35;1mCACHE (0.0ms)[0m   [0mSELECT * FROM "users" WHERE ("users"."id" = '4') LIMIT 1[0m
    [4;36;1mCACHE (0.0ms)[0m   [0;1mSELECT * FROM "users" WHERE ("users"."facebook_uid" = 100001121293021) LIMIT 1[0m
    [4;35;1mCACHE (0.0ms)[0m   [0mSELECT "users".id FROM "users" WHERE ("users"."persistence_token" = 'ddd6b0d160321e55266db847d0b4558e9ed76ab220ef3fce655c24c5d24286d56d728deda76b44741121b0b78e1c266537fbfe00bf01206a393090c5c1f475' AND "users".id <> 4) LIMIT 1[0m
    [4;36;1mCACHE (0.0ms)[0m   [0;1mSELECT * FROM "users" WHERE ("users"."id" = '4') LIMIT 1[0m
    [4;35;1mCACHE (0.0ms)[0m   [0mSELECT * FROM "users" WHERE ("users"."facebook_uid" = 100001121293021) LIMIT 1[0m
    [4;36;1mCACHE (0.0ms)[0m   [0;1mSELECT "users".id FROM "users" WHERE ("users"."persistence_token" = 'ddd6b0d160321e55266db847d0b4558e9ed76ab220ef3fce655c24c5d24286d56d728deda76b44741121b0b78e1c266537fbfe00bf01206a393090c5c1f475' AND "users".id <> 4) LIMIT 1[0m
    [4;35;1mCACHE (0.0ms)[0m   [0mSELECT * FROM "users" WHERE ("users"."id" = '4') LIMIT 1[0m
    [4;36;1mCACHE (0.0ms)[0m   [0;1mSELECT * FROM "users" WHERE ("users"."facebook_uid" = 100001121293021) LIMIT 1[0m
    [4;35;1mCACHE (0.0ms)[0m   [0mSELECT "users".id FROM "users" WHERE ("users"."persistence_token" = 'ddd6b0d160321e55266db847d0b4558e9ed76ab220ef3fce655c24c5d24286d56d728deda76b44741121b0b78e1c266537fbfe00bf01206a393090c5c1f475' AND "users".id <> 4) LIMIT 1[0m
    [4;36;1mCACHE (0.0ms)[0m   [0;1mSELECT * FROM "users" WHERE ("users"."id" = '4') LIMIT 1[0m
    [4;35;1mCACHE (0.0ms)[0m   [0mSELECT * FROM "users" WHERE ("users"."facebook_uid" = 100001121293021) LIMIT 1[0m
    [4;36;1mCACHE (0.0ms)[0m   [0;1mSELECT "users".id FROM "users" WHERE ("users"."persistence_token" = 'ddd6b0d160321e55266db847d0b4558e9ed76ab220ef3fce655c24c5d24286d56d728deda76b44741121b0b78e1c266537fbfe00bf01206a393090c5c1f475' AND "users".id <> 4) LIMIT 1[0m
    [4;35;1mCACHE (0.0ms)[0m   [0mSELECT * FROM "users" WHERE ("users"."id" = '4') LIMIT 1[0m
    [4;36;1mCACHE (0.0ms)[0m   [0;1mSELECT * FROM "users" WHERE ("users"."facebook_uid" = 100001121293021) LIMIT 1[0m
    [4;35;1mCACHE (0.0ms)[0m   [0mSELECT "users".id FROM "users" WHERE ("users"."persistence_token" = 'ddd6b0d160321e55266db847d0b4558e9ed76ab220ef3fce655c24c5d24286d56d728deda76b44741121b0b78e1c266537fbfe00bf01206a393090c5c1f475' AND "users".id <> 4) LIMIT 1[0m
    

1 个答案:

答案 0 :(得分:0)

知道了! 在使用authlogic进行正常用户注册时,会发生以下情况: before_validation:reset_persistence_token,:if =&gt; :reset_persistence_token? 这样做,如果它是空白的话,设置一个初始的persistence_token

出于某种原因,authlogic_facebook_connect插件会跳过此验证,导致将NULL persistence_token字段发送到数据库。

在我的用户表格中,我有字段persistence_token

t.string :persistence_token

应该像这样定义,但要确保它不是null:

t.string :persistence_token, :null => false

现在,您不会挂起浏览器,而是会收到sqlite错误,指出持久性令牌为空。那更好,现在我们知道发生了什么。

转到位于vendor / plugins / authlogic_facebook_connect / lib / authlogic_facebook_connect / session.rb内的authlogic_facebook_connect插件的validate_by_facebook_connect方法,其中显示

new_user.send(:"#{facebook_uid_field}=", facebook_session.user.uid)
new_user.send(:"#{facebook_session_key_field}=", facebook_session.session_key)

添加另一行,如下所示:

new_user.send(:"#{facebook_uid_field}=", facebook_session.user.uid)
new_user.send(:"#{facebook_session_key_field}=", facebook_session.session_key)
new_user.reset_persistence_token

现在我们确保在第一次创建用户时设置了持久性令牌。 再做一次,浏览器不会挂起,用户被创建,欢迎使用authlogic facebook connect setup。

即使我在authlogic_facebook_connect插件中包含了修复程序,但这确实是一个authlogic问题。


修改

  1. 我提交了一张票给 authlogic github page关于 这个问题
  2. 我写了一篇关于这个问题的blog post来帮助其他人坚持下去。