无法获取 current_user 设备和 oAuth facebook
Cant get current_user Device and oAuth facebook
我仅使用 Omniauth 从 Facebook 登录用户
** Users::OmniauthCallbackController**
class Users::OmniauthCallbacksController <
设计::OmniauthCallbacksController
def facebook
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication
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
def failure
redirect_to games_path
end
结束
但是当我尝试将 current_user 发送到另一个控制器时,它总是 returns nil。
我读到设计有一个内置函数 current_user 但它似乎不能正常工作(每次都返回 nil)所以我实现了另一种方法。
current_user帮手
before_action :define_current_user
def current_user
User.find_by id: session["current_user_id"]
end
helper_method :current_user
我怎样才能让它发挥作用?
User.rb
class User < ApplicationRecord
belongs_to :team
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :rememberable, :trackable,:validatable
,:omniauthable, :omniauth_providers => [:facebook]
def self.new_with_session(params,session)
super.tap do |user|
if data = session["devise.facebook_data"] &&
session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
#Saving new user to the database
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do|user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name
user.image = auth.info.avatar
user.save!
end
end
end
不要忘记向需要的控制器添加 before_action :authenticate_user!
回调。在这种情况下,您将能够从 Devise
获得默认值 current_user
我仅使用 Omniauth 从 Facebook 登录用户
** Users::OmniauthCallbackController**
class Users::OmniauthCallbacksController <
设计::OmniauthCallbacksController
def facebook
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication
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
def failure
redirect_to games_path
end
结束
但是当我尝试将 current_user 发送到另一个控制器时,它总是 returns nil。
我读到设计有一个内置函数 current_user 但它似乎不能正常工作(每次都返回 nil)所以我实现了另一种方法。
current_user帮手
before_action :define_current_user
def current_user
User.find_by id: session["current_user_id"]
end
helper_method :current_user
我怎样才能让它发挥作用?
User.rb
class User < ApplicationRecord
belongs_to :team
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :rememberable, :trackable,:validatable
,:omniauthable, :omniauth_providers => [:facebook]
def self.new_with_session(params,session)
super.tap do |user|
if data = session["devise.facebook_data"] &&
session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
#Saving new user to the database
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do|user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name
user.image = auth.info.avatar
user.save!
end
end
end
不要忘记向需要的控制器添加 before_action :authenticate_user!
回调。在这种情况下,您将能够从 Devise
current_user