ajaxでユーザーにサインインしようとするので、sign_in_and_redirectを使用する代わりにsign_inメソッドを使用しました。ajax、devise、omniauthを使用しているときにユーザーがサインインできない
関数の最後にsigned_in_user?期待どおりに真ですが、サーバーに再度アクセスすると、signed_in_user?実際には偽です。 私に何かが不足していますか?
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
respond_to :json
def all_omniauth_providers
@user = User.find_for_oauth(request.env["omniauth.auth"])
if @user.persisted?
# sign_in_and_redirect @user, :event => :authentication
if (sign_in(@user, :event => :authentication))
respond_with resource, location: after_sign_in_path_for(resource)
else
# respond_with resource, location: root_path
end
else
session["devise.#{provider}_data"] = request.env["omniauth.auth"]
render :status => 401, :json => { :errors => alert }
end
end
alias_method :facebook, :all_omniauth_providers
end
と接続するためのjavascript:
<script type="text/javascript">
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : '<%=Rails.application.secrets.facebook_app_id%>', // App ID from the App Dashboard
channelUrl : '//localhost:3000/channel.html', // Channel File for x-domain communication
status : true, // check the login status upon init?
cookie : true, // set sessions cookies to allow your server to access the session?
xfbml : true, // parse XFBML tags on this page?
version : 'v2.5'
});
// Additional initialization code such as adding Event Listeners goes here
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
$(function() {
$('#facebook-connect').click(function(e) {
e.preventDefault();
FB.login(function(response) {
if(response.authResponse) {
$.ajax({
type: 'POST',
url: '/users/auth/facebook/callback',
dataType: 'json',
data: {signed_request: response.authResponse.signedRequest},
success: function(data, textStatus, jqXHR) {
$('#close').click();
$("#share").jsSocials("option", "url", "<%=url%>?ref=" + data.id);
$('#loginout_button').html("<%= j button_to t('auth.logout'), destroy_user_session_path, :class=>'btn btn-default navbar-btn navbar-left btn-xs btn-success', :method => :delete %>");
},
error: function(jqXHR, textStatus, errorThrown) {
$('.server_error').addClass('alert alert-danger').html("<%= t('auth.login_failed') %>");
shakeModal();
}
});
}
}
, {scope: 'email'});
});
});
$(function() {
$('#facebook-logout').click(function(e) {
e.preventDefault();
FB.getLoginStatus(function(response) {
if(response.authResponse) {
FB.logout();
}
}
, {scope: 'email'});
});
});
</script>
<!-- end Facebook connect script -->
おそらくサインインを行うJSを含める必要があります。私はヘッダー/クッキー/を疑うだろうか?クライアントに設定されていません。 –
はjavascriptログイン機能で更新されました。奇妙なことは、一度ページを更新すると、サーバーはuser_signed_inを返しますか?期待通りの真実を... – user664859