summaryrefslogtreecommitdiff
path: root/views/settings.php
blob: 61feefc62786bb0e8e5048ef15af92f5136898eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<div class="narrow">
  <?= partial('partials/header') ?>

  <h2>Signed In As</h2>
  <code><?= session('me') ?></code>

  <h3>Facebook</h3>
  <input type="button" id="facebook-button" value="Checking" class="btn">

  <h3>Twitter</h3>
  <input type="button" id="twitter-button" value="Checking" class="btn">


  <!--
  <h3>Instagram</h3>

  -->

</div>
<script>
window.quillFbInit = function() {
  FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
      // the user is logged in and has authenticated your
      // app, and response.authResponse supplies
      // the user's ID, a valid access token, a signed
      // request, and the time the access token 
      // and signed request each expire
      var uid = response.authResponse.userID;
      var accessToken = response.authResponse.accessToken;

      save_facebook_token(response.authResponse.accessToken);

    } else if (response.status === 'not_authorized') {
      // the user is logged in to Facebook, 
      // but has not authenticated your app
      console.log("Logged in but not authorized");

      $("#facebook-button").val("Sign In").addClass("btn-warning");

    } else {
      // the user isn't logged in to Facebook.
      console.log("User isn't logged in");

      $("#facebook-button").val("Sign In").addClass("btn-warning");
    }
  });
};
window.quillHandleFbLogin = function(response) {
  save_facebook_token(response.authResponse.accessToken);
};

function save_facebook_token(token) {
  console.log("Authed with token: " + token);
  $.post('/auth/facebook', {
    fb_token: token
  }, function(data){
    $("#facebook-button").val("Connected").addClass("btn-success");
  });
}

$(function(){
  $.getJSON("/auth/twitter", function(data){
    // Check if we're already authorized with twitter
    if(data && data.result == 'ok') {
      $("#twitter-button").val("Connected").addClass("btn-success");
    } else if(data && data.url) {
      $("#twitter-button").val("Sign In").data("url", data.url).addClass("btn-warning");
    } else {
      $("#twitter-button").val("Error").addClass("btn-danger");
    }
  });

  $("#twitter-button").click(function(){
    if($(this).data('url')) {
      window.location = $(this).data('url');
    } else {
      $.getJSON("/auth/twitter", {login: 1}, function(data){
        window.location = data.url;
      });
    }
  });

  $("#facebook-button").click(function(){
    FB.login(window.quillHandleFbLogin, {scope:'publish_actions'});
  });
});
</script>