我已将omniauth-facebook集成到我的应用程序中以验证用户身份。它工作正常,但有一个问题,用户没有作为设计用户登录。我正在关注本教程Devise omniauth integration 我面临的问题很多。第一件事是签名用户应该为的所有条件都是假的,如
if user_signed_in?
为false current_user
为空,这意味着用户在经过身份验证后未登录。
Omniauth控制器回调方法代码是
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(:user, @user)
redirect_to :controller => '/donations', :action => 'donor_history'
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
应用程序控制器代码就是这个
class ApplicationController < ActionController::Base
add_flash_types :success, :warning, :danger, :info
protect_from_forgery prepend: true
before_filter :configure_permitted_parameters, if: :devise_controller?
layout :layout_by_resource
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
private
def layout_by_resource
if devise_controller?
"admin"
else
"application"
end
end
protected
def after_sign_in_path_for(resource)
#request.env['omniauth.origin'] || root_path
if(resource.admin==false)
'/donations/donor_history'
else
'/admins/' #your path
end
end
def after_sign_out_path_for(resource)
'/users/sign_in' #your path
end
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) do |user_params|
user_params.permit(:admin, :email, :password, :password_confirmation,:first_name,:last_name)
end
devise_parameter_sanitizer.permit(:account_update) do |user_params|
user_params.permit(:admin, :email, :password, :password_confirmation,:current_password,:first_name,:last_name)
end
end
end
任何帮助都将受到高度赞赏。谢谢