在我的 session_controller.rb 我有这个:
require 'linkedin'
class SessionController < ApplicationController
def connect
# get your api keys at https://www.linkedin.com/secure/developer
client = LinkedIn::Client.new(APP_CONFIG['linkedin']['apikey'], APP_CONFIG['linkedin']['secret_key'])
request_token = client.request_token(:oauth_callback =>
"http://#{request.host_with_port}/session/callback")
session[:rtoken] = request_token.token
session[:rsecret] = request_token.secret
redirect_to client.request_token.authorize_url
end
def callback
client = LinkedIn::Client.new(APP_CONFIG['linkedin']['apikey'], APP_CONFIG['linkedin']['secret_key'])
if session[:atoken].nil?
pin = params[:oauth_verifier]
atoken, asecret = client.authorize_from_request(session[:rtoken], session[:rsecret], pin)
session[:atoken] = atoken
session[:asecret] = asecret
else
client.authorize_from_access(session[:atoken], session[:asecret])
end
redirect_to 'users/index'
end
end
这很好用,我现在的问题是如何检查“用户/索引”操作是否用户已经通过了 Linkedin 的 OAuth 流程?这是我正在尝试做的事情的开始:
class UsersController < ApplicationController
respond_to :html, :js, :json
def index
if authenticated?
# set up a new LinkedIn client and show profile data
else
redirect_to 'session/connect'
end
end
...
private
def authenticated?
# what should i do here?
# return true if the user authenticated
# return false if not
end
我知道我可能应该检查会话中是否设置了一些值,但我不确定我正在寻找哪一个。我知道将来我可能会移动“经过身份验证?” 方法,所以它在所有视图上调用,但这没关系。
有什么帮助吗?谢谢!