3

我试图允许用户通过 facebook 登录,而不是被重定向到当前页面。我目前正在使用带有omniauth-facebook 的Rails 并设计用于身份验证。我假设最好的方法是通过 ajax 一旦我通过 Javascript api 收到来自 facebook 的身份验证。但是,我不确定需要将什么传递给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

0

对我来说,问题是发出 GET 请求而不是 POST 请求。改成 POST 问题就解决了。

我会把它作为评论留下,因为我不会留下更多信息来证明这个解决方案的合理性,但我是新手(StackOverflow);我没有足够的积分来发表评论。

无论如何,希望它有所帮助!

于 2017-03-08T05:54:37.047 回答