3

ユーザーが Facebook 経由でサインインできるようにし、現在のページからリダイレクトされないようにしようとしています。現在、Rails と omniauth-facebook を使用して認証を行っています。Javascript APIを介してFacebookから認証を受け取ったら、これを行う最善の方法はajaxを介することだと思います。ただし、認証を確認するために omniauth のコールバック URL に何を渡す必要があるのか​​ わかりません。これが私が現在持っているものです(当面はjqueryの使用を避けようとしています)

:javascript

  window.fbAsyncInit = function() {

    // init the FB JS SDK
    FB.init({
      appId      : 'app-id',                           // App ID from the app dashboard
      channelUrl : '//localhost:3000/channel.html',    // Channel file for x-domain comms
      status     : true,                               // Check Facebook Login status
      cookie     : true,
      xfbml      : true                                // Look for social plugins on the page
    });

    // Additional initialization code such as adding Event Listeners goes here
    document.getElementById('facebook-login').onclick = function(event) {
      FB.login(function(response) {

        if (response.authResponse) {
          var xhr = new XMLHttpRequest();
          xhr.onreadystatechange = function() {
            console.log(xhr);
            if (xhr.readyState == 4) {
              console.log(xhr.responseText);
            }
          }
          xhr.open('GET', 'http://localhost:3000/users/auth/facebook/callback', true);
          xhr.send(null);

        } else {
          console.log("Something when horrible wrong");
        }
      }, {scope: ''});
    }
  };

  // Load the SDK asynchronously
  (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/all.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));

私が最も確信が持てないのは、自分のエンドポイント (users/auth/facebook/callback) を正しく呼び出しているかどうか、またはそれに何かを渡す必要があるかどうかです。どんな助けでも大歓迎です。

4

1 に答える 1