提问人:RABEYA KHATUN MUNA 提问时间:9/29/2022 最后编辑:RABEYA KHATUN MUNA 更新时间:10/2/2022 访问量:169
rails中Facebook SSO回调错误的含义是什么
What is the meaning of the error for Facebook SSO callback in rails
问:
我正在尝试在没有任何宝石的项目中实现 facebook sso。回调后,它会抛出以下错误。
API 调用
错误
gem 'devise'
gem 'devise_invitable', '~> 2.0.0'
gem 'devise-jwt'
gem 'omniauth-oauth2', '~> 1.7'
gem 'omniauth'
gem 'omniauth-rails_csrf_protection', '~> 1.0.0'
OmniauthAuthenticatorsController.rb
def facebook
ForceSignOut.call(request) if request.cookie_jar["#{tenant.upcase}-ID-TOKEN"].present?
repost("/auth/facebook/#{omniauth_params}",
options: { authenticity_token: :auto, cookies: cookies })
end
omniauth_callbacks_controller.rb
def facebook_hotwire
success(CncOmniauth::FacebookSession.new(request, auth_hash).authenticate)
end
脸书.rb
# frozen_string_literal: true
require 'omniauth-oauth2'
module OmniAuth
module Strategies
class Facebook < OmniAuth::Strategies::OAuth2
DEFAULT_SCOPE = 'email'
option :name, :facebook
option :client_options, {
site: 'https://graph.facebook.com/v4.0',
authorize_url: 'https://www.facebook.com/v4.0/dialog/oauth',
token_url: 'oauth/access_token'
}
option :authorize_options, [:scope]
uid { raw_info['id'] }
extra do
{ 'raw_info' => raw_info }
end
def raw_info
@raw_info ||= access_token.get("#{SOCIAL['facebook']['api_endpoint']}?fields=#{SOCIAL['facebook']['fields']}").parsed || {}
end
def authorize_params
super.tap do |params|
params['scope'.to_sym] = request.params['scope'] if request.params['scope']
params[:scope] ||= DEFAULT_SCOPE
session['omniauth.state'] = params[:state] = CncOmniauth::FacebookSession.state(request.params)
end
end
def callback_url
options[:redirect_uri] || (full_host + script_name + callback_path)
end
end
end
end
facebook_session.rb
# frozen_string_literal: true
module CncOmniauth
class FacebookSession < Base
attr_accessor :extra, :info
def authenticate
ApartmentService.switch state_params['tenant']
return register_user && { user: user.detail, state: state_params } if user.blank?
set_cookie && save_session && save
{ user: user.list, state: state_params }
end
private
def user
@user ||= User.find_by(email: info['email'])
end
def provider
'facebook'
end
def access_token
credentials['token']
end
def refresh_token
credentials['refresh_token']
end
def expiry
Time.zone.at(credentials['expires_at'])
end
class << self
def state(params)
JWT.encode(
{
tenant: params['tenant'] || Cnc::Scope::Tenant.current,
role_id: params['role_id'],
redirect_url: params['redirect_url'],
retry_count: params['retry_count']
}, secret
)
end
end
end
end
omniauth_graph.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook,
ENV['FACEBOOK_APP_ID'],
ENV['FACEBOOK_APP_SECRET']
end
OmniAuth.config.allowed_request_methods = [:post, :get]
routes.rb (路由.rb)
match 'auth/facebook_hw/callback', to: 'api/v2/iam/users/omniauth_callbacks#facebook_hotwire', via: %i[get post]
get 'omniauth/facebook_hw_sign_in', to: 'api/v2/iam/users/omniauth_authenticators#facebook'
前端部分:
facebook(event) {
let data = `?tenant=${event.currentTarget.dataset.tenant}&redirect_url=https://${this.element.dataset.omni_auth}&role_id=3`
window.location.href = `/omniauth/facebook_hw_sign_in/${data}`
}
this.element.dataset.omni_auth = 站点 URL event.currentTarget.dataset.tenant = 租户名称
错误的含义是什么?
答:
0赞
Adam Zapaśnik
10/2/2022
#1
我在 routes.rb 中看到,但在浏览器中我看到.如果您在浏览器中将 url 编辑为使用会发生什么,它有效吗?match 'auth/facebook_hw/callback'
auth/facebook/callback
_hw
评论
0赞
RABEYA KHATUN MUNA
10/2/2022
是的,如果我在浏览器中添加facebook_hw,它将进入端点。但是我应该在 omniauth 控制器还是在元开发者控制台中添加facebook_hw?
0赞
Adam Zapaśnik
10/2/2022
这是一个回调端点,对吧?Facebook 使用可用于获取访问令牌的 Facebook 重定向到它。 一个应该在元开发者控制台中code
facebook_hw
评论