设计 gem 使用 facebook 登录获取“您已经登录。”登录或注册后
Devise gem sign in with facebook get " You are already signed in." after sign in or sign up
我使用以下 gem 登录/注册我的应用程序
# Devise is a flexible authentication solution for Rails based on Warden. It:
gem 'devise'
#using omniauth
gem 'omniauth'
#facebook authentication using omniauth
gem 'omniauth-facebook'
但在用户登录或注册后,我总是得到闪光灯:
You are already signed in.
这是我的用户模型:
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]
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 # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
end
end
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
end
我的用户控制器:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@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
有什么想法或建议吗?
我遇到了同样的问题。解决方案是更改 after_sign_in_path_for
方法。例如 ApplicationController
:
def after_sign_in_path_for(resource)
super resource
end
我使用以下 gem 登录/注册我的应用程序
# Devise is a flexible authentication solution for Rails based on Warden. It:
gem 'devise'
#using omniauth
gem 'omniauth'
#facebook authentication using omniauth
gem 'omniauth-facebook'
但在用户登录或注册后,我总是得到闪光灯:
You are already signed in.
这是我的用户模型:
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]
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 # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
end
end
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
end
我的用户控制器:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@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
有什么想法或建议吗?
我遇到了同样的问题。解决方案是更改 after_sign_in_path_for
方法。例如 ApplicationController
:
def after_sign_in_path_for(resource)
super resource
end