-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
119 lines (101 loc) · 2.54 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*** Init VUE ***/
const app = new Vue({
el: '#app',
data: {
authenticated: false,
user: {
email: "",
name: "",
avatar_url: ""
},
provider: "",
googleSdkReady: false
},
methods: {
logout: function () {
this.authenticated = false;
if (this.provider == "Facebook") {
FB.logout(function (response) {
history.pushState({}, null, "/");
});
} else if (this.provider == "Google") {
gapi.auth2.getAuthInstance().signOut().then(
function (response) {
history.pushState({}, null, "/");
}
);
} else { // SimpleLogin
history.pushState({}, null, "/");
}
}
}
});
/*** SimpleLogin, using JSO library ***/
// Make an authorization request if the user click the login button.
SL.init("demo");
function simpleLogin() {
SL.login(function(user){
app.authenticated = true;
app.provider = "SimpleLogin";
app.user = {
email: user.email,
name: user.name,
avatar_url: user.avatar_url
};
});
}
/*** END SimpleLogin ***/
/*** Facebook ***/
function getFBUserData() {
FB.api('/me?fields=id,name,email,picture{url}', function (response) {
// response has this form {"id":"1234","name":"First Last","email":"abcd@gmail.com","picture":{"data":{"url":"https://avatar.png"}}}
app.authenticated = true;
app.provider = "Facebook";
app.user = {
email: response.email,
name: response.name,
avatar_url: response.picture.data.url
};
});
}
function fbLogin() {
FB.login(function (response) {
if (response.status === 'connected')
getFBUserData();
});
}
FB.getLoginStatus(function (response) {
if (response.status === 'connected')
getFBUserData();
});
/*** END Facebook ***/
/*** Google ***/
function googleLogin() {
gapi.auth2.getAuthInstance().signIn().then(
function (googleUser) {
var profile = googleUser.getBasicProfile();
app.authenticated = true;
app.provider = "Google";
app.user = {
email: profile.getEmail(),
name: profile.getName(),
avatar_url: profile.getImageUrl()
};
},
function (error) {
console.log(error);
}
);
}
function initGoogle() {
gapi.load('auth2', function () {
gapi.auth2.init({
client_id: '606630287997-3jlo7tlte0jktmavbv3rj0nav71fiec2.apps.googleusercontent.com'
}).then(function () {
console.log("Google Sdk finally ready");
app.googleSdkReady = true;
});
});
}
initGoogle();
/*** End Google ***/