简体   繁体   English

session [:user_id]即使用户登录Rails也返回nil

[英]session[:user_id] returning nil even if user signed in Rails

omniauth_callbacks_controller omn​​iauth_callbacks_controller

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

def facebook

 @user = User.from_omniauth(request.env["omniauth.auth"])

 if @user.persisted?
  sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
  set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
 else
  session["devise.facebook_data"] = request.env["omniauth.auth"]
  redirect_to new_user_registration_url
  end
 end
end

session_controller session_controller

class SessionsController < ApplicationController

 def create
  user = User.from_omniauth(env["omniauth.auth"])
  #sign_in(:user, user)
  session[:user_id] = user.id 
  redirect_to root_url
 end

 def destroy
  session[:user_id] = nil
  redirect_to root_url
 end
end

application_controller application_controller

 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
    private
    helper_method :current_user
    def current_user
         @current_user = User.find(session[:user_id])  
    end

  end

user.rb user.rb

   class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable,:omniauthable, :omniauth_providers => [:facebook]
      belongs_to :restaurant, foreign_key: 'restaurant_id'
      has_many :orders
      def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.name = auth.info.name
        user.oauth_token = auth.credentials.token
        user.email = auth.info.email
        user.password = Devise.friendly_token[0,20]
        user.oauth_expires_at = Time.at(auth.credentials.expires_at)

      end
  end
end

visitor_controller visitor_controller

 class VisitorsController < ApplicationController

   def index
     @user = current_user

   end
 end

This is the root page controller of my app. 这是我的应用程序的根页面控制器。 I am creating a restaurant ordering app, but the problem is all orders are being displayed to all user. 我正在创建一个餐厅订购应用程序,但是问题是所有订单都显示给所有用户。

So i need to make each order user specific. 所以我需要使每个订单用户特定。 But I am not being able to access 'user.id' from any of my controller. 但是我无法从任何控制器访问“ user.id”。

Please help. 请帮忙。 Not being able to find the problem. 无法找到问题。 Thank you. 谢谢。

If you have an application where you intend to use Devise and OmniAuth together you do not need to create a custom SessionsController . 如果您有一个打算将Devise和OmniAuth一起使用的应用程序,则无需创建自定义SessionsController

You would simply need customize to the login page with a link: 您只需要通过链接自定义登录页面即可

<%= link_to "/auth/facebook", "Sign in with Facebook" %>

Note that the same callback URL will be used for both new and returning users! 请注意,新用户和返回用户都将使用相同的回调URL!

sign_in_and_redirect in your Users::OmniauthCallbacksController in will handle serialing the user in the session with the help of Warden and you can sign out user by the normal Devise controller. sign_in_and_redirect在你的用户:: OmniauthCallbacksController将处理serialing在典狱长的帮助会话的用户,您可以通过正常的设计控制器登出用户。


Edit 编辑

The biggest problem with you seem to have is understanding how OmniAuth and Devise work together. 您似乎遇到的最大问题是了解OmniAuth和Devise如何一起工作。

Devise provides a normal user / password signup and all the functionality needed for sessions. Devise提供正常的用户/密码注册以及会话所需的所有功能。

OmniAuth provides abstraction for dealing with different OAuth providers and dealing with details of OAuth. OmniAuth提供了与不同的OAuth提供程序进行处理以及处理OAuth的详细信息的抽象。

All your callbacks controller need to do is call Devises sign_in_and_redirect method and it will take care of serializing the user in the session through Warden. 您的回调控制器所需要做的就是调用Devises sign_in_and_redirect方法,它将负责通过Warden在会话中序列化用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM