使用Janrain和Ruby on Rails访问当前用户信息

时间:2012-09-02 01:02:46

标签: ruby-on-rails janrain

我使用Janrain在Ruby on Rails应用程序中处理用户会话。它似乎正在工作,但是,我不知道如何判断用户是否登录或访问当前用户的信息。用户登录后,是否创建了会话变量?

2 个答案:

答案 0 :(得分:1)

假设您指的是Janrain社交登录(Engage),一旦用户通过社交提供商进行身份验证,该小部件就会获得一个有效60分钟的Janrain OAuth令牌。您可以使用该令牌通过此API端点检索用户的配置文件数据:(https:// {your-engage-domain.com} / api / v2 / auth_info)。

Janrain Social Login不会维护任何登录状态相关的会话数据。它只是简化了身份验证,并规范了从多个身份验证提供程序中检索用户配置文件数一旦成功的身份验证事件发生,您的服务器将验证身份验证令牌,然后建立任何形式的授权会话相关工作。

大多数社交提供商都会返回有效期为30-60天的访问权限。

答案 1 :(得分:0)

尝试'current_user'变量,它适用于大多数rails认证库,例如:

#in the erb file: 
<% current_user = session[:user_id] %>

# or in the rb file: 
class MusicController < ApplicationController
  before_filter :authenticate_user! # just like devise

  def index
    # same methods and api as devise.
    return if signed_in? and current_user.email
  end
end

# put this method in application_controller.rb
def current_user
  @current_user ||= User.find_by_id(session[:user_id])
end

更多细节请参考此示例:https://github.com/hatem/janrain-engage-demo