-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.js
95 lines (74 loc) · 2.13 KB
/
main.js
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
89
90
91
92
93
94
95
$(function() {
var app_id = '1682310898660786';
var scopes = 'email, user_friends, user_online_presence';
var btn_login = '<a href="#" id="login" class="btn btn-primary">Iniciar sesión</a>';
var div_session = "<div id='facebook-session'>"+
"<strong></strong>"+
"<img>"+
"<a href='#' id='logout' class='btn btn-danger'>Cerrar sesión</a>"+
"</div>";
window.fbAsyncInit = function() {
FB.init({
appId : app_id,
status : true,
cookie : true,
xfbml : true,
version : 'v2.1'
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response, function() {});
});
};
var statusChangeCallback = function(response, callback) {
console.log(response);
if (response.status === 'connected') {
getFacebookData();
} else {
callback(false);
}
}
var checkLoginState = function(callback) {
FB.getLoginStatus(function(response) {
callback(response);
});
}
var getFacebookData = function() {
FB.api('/me', function(response) {
$('#login').after(div_session);
$('#login').remove();
$('#facebook-session strong').text("Bienvenido: "+response.name);
$('#facebook-session img').attr('src','http://graph.facebook.com/'+response.id+'/picture?type=large');
});
}
var facebookLogin = function() {
checkLoginState(function(data) {
if (data.status !== 'connected') {
FB.login(function(response) {
if (response.status === 'connected')
getFacebookData();
}, {scope: scopes});
}
})
}
var facebookLogout = function() {
checkLoginState(function(data) {
if (data.status === 'connected') {
FB.logout(function(response) {
$('#facebook-session').before(btn_login);
$('#facebook-session').remove();
})
}
})
}
$(document).on('click', '#login', function(e) {
e.preventDefault();
facebookLogin();
})
$(document).on('click', '#logout', function(e) {
e.preventDefault();
if (confirm("¿Está seguro?"))
facebookLogout();
else
return false;
})
})