从 Devise、Ruby on Rails 获取用户 ID

发布于 2024-11-27 06:23:11 字数 757 浏览 1 评论 0原文

我使用 Devise 和基本 HTTP 身份验证从移动应用程序访问用户区域。当用户在应用程序中输入其详细信息并尝试“登录”时(当您随每个请求发送凭据时,您实际上并没有使用基本身份验证登录),我希望收到包含用户信息的响应,例如用户 ID和电子邮件。

当我尝试获取资源时,我得到如下响应:

[
 -
 {
    email: "[email protected]"
    username: "Example"
 }
 -
  [
  -
  {
     created_at: "2011-07-26T11:30:55Z"
     id: 1
     title: "Test"
     updated_at: "2011-07-26T11:30:55Z"
     user_id: 1
  }
  -
  {
     created_at: "2011-07-26T16:53:26Z"
     id: 2
     title: "Test2"
     updated_at: "2011-07-26T16:53:26Z"
     user_id: 1
  }
 ]
]

所以我一开始就获取了用户对象。但是,我还需要添加用户 ID。我无法在项目中使用 user_id,因为如果用户没有项目,则不会呈现该 user_id。

有什么建议吗?

I use Devise and Basic HTTP Authentication to access user areas from a mobile app. When the user enters its details in the app and it tries to "login" (you don't actually login with basic auth as you send the credentials with each request) I want to get a response back with the user info, like user ID and email.

When I try to get a resource I get a response like this:

[
 -
 {
    email: "[email protected]"
    username: "Example"
 }
 -
  [
  -
  {
     created_at: "2011-07-26T11:30:55Z"
     id: 1
     title: "Test"
     updated_at: "2011-07-26T11:30:55Z"
     user_id: 1
  }
  -
  {
     created_at: "2011-07-26T16:53:26Z"
     id: 2
     title: "Test2"
     updated_at: "2011-07-26T16:53:26Z"
     user_id: 1
  }
 ]
]

So I get the user object at the beginning. However, I need to add the user ID as well. I can't use the user_id in the items because that won't be rendered if the user has no items.

Any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放飞的风筝 2024-12-04 06:23:11

您可能需要扩展 Devise::SessionsConroller。在您的控制器文件夹中,添加一个用户文件夹,然后添加一个名为 sessions_controller.rb 的文件,其中包含以下内容:

class Users::SessionsController < Devise::SessionsController
  def new
    super
    render :json => {:id => current_user.id}.to_json
  end
end

您的routes.rb中:

  devise_for  :users, 
              :controllers => {:sessions => 'users/sessions'}

编辑

尝试此操作。从 routes.rb 中取出该行。在 application_controller.rb 中添加以下内容:

  # Devise override
  def after_sign_in_path_for(resource) 
    render :json => {:id => resource.id}.to_json
  end

You probably need to extend Devise::SessionsConroller. In your controllers folder, add a users folder and then add a file called sessions_controller.rbthat contains this:

class Users::SessionsController < Devise::SessionsController
  def new
    super
    render :json => {:id => current_user.id}.to_json
  end
end

In your routes.rb:

  devise_for  :users, 
              :controllers => {:sessions => 'users/sessions'}

EDIT

Try this. Take that line out of your routes.rb. In application_controller.rb add this:

  # Devise override
  def after_sign_in_path_for(resource) 
    render :json => {:id => resource.id}.to_json
  end
恍梦境° 2024-12-04 06:23:11

经过一个下午的努力寻找一个好的答案,我发现了这个:

资源:http://rails-bestpractices.com/ posts/47-fetch-current-user-in-models

希望有帮助!

after an aftternoon to try to find a good answer, i've found this:

Resource: http://rails-bestpractices.com/posts/47-fetch-current-user-in-models

Hope it Helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文