簡體   English   中英

如何在Rails 4中使用Omniauth-facebook對用戶進行身份驗證

[英]How to authenticate user with Omniauth-facebook in rails 4

我已經開始開發我的Rails應用程序,並且用戶將僅使用facebook即omniauth-facebook gem登錄。 但是現在我該如何使用user_signed_in? 在我的應用中不使用devise的方法?

我想在我的應用中的每項操作之前對用戶進行身份驗證。

在ApplicationController中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM