如何在 rails 4 中使用 Omniauth-facebook 验证用户
How to authenticate user with Omniauth-facebook in rails 4
我已经开始开发我的 rails 应用程序,用户将仅使用 facebook 登录,即 omniauth-facebook gem
。但是现在,如果不在我的应用程序中使用设计,我怎么能拥有类似 user_signed_in?
的方法呢?
我想在我的应用程序中的每个操作之前对用户进行身份验证。
在应用程序控制器中:
class ApplicationController < ActionController::Base
#Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
protected
def authenticate!
redirect_to root_path unless user_signed_in?
end
def user_signed_in?
!!session[:user_id]
end
def current_user
User.find(session[:user_id])
end
end
通过将方法放在 ApplicationController 中,您可以在任何控制器中调用它们。现在,要在您的视图中使用它们,您必须将此代码复制到您的 ApplicationHelper
module ApplicationHelper
def user_signed_in?
!!session[:user_id]
end
def current_user
User.find(session[:user_id])
end
end
我已经开始开发我的 rails 应用程序,用户将仅使用 facebook 登录,即 omniauth-facebook gem
。但是现在,如果不在我的应用程序中使用设计,我怎么能拥有类似 user_signed_in?
的方法呢?
我想在我的应用程序中的每个操作之前对用户进行身份验证。
在应用程序控制器中:
class ApplicationController < ActionController::Base
#Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
protected
def authenticate!
redirect_to root_path unless user_signed_in?
end
def user_signed_in?
!!session[:user_id]
end
def current_user
User.find(session[:user_id])
end
end
通过将方法放在 ApplicationController 中,您可以在任何控制器中调用它们。现在,要在您的视图中使用它们,您必须将此代码复制到您的 ApplicationHelper
module ApplicationHelper
def user_signed_in?
!!session[:user_id]
end
def current_user
User.find(session[:user_id])
end
end