-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfacebookAuth.js
108 lines (105 loc) · 5.27 KB
/
facebookAuth.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
/*
* Filename: d:\code\src\github.com\loitd\vue-quick-template\src\modules\vue-all-auth\facebookAuth.js
* Path: d:\code\src\github.com\loitd\vue-quick-template\src\modules\vue-all-auth
* Created Date: Wednesday, March 13th 2019, 5:58:27 pm
* Author: loi_t
*
* Copyright (c) 2019 Your Company
*/
let facebookAuth = {
// Define functions
fbInstallClient: function(){
// Installing client mean append client script to head (and make sure it loaded)
console.log("Begin installing FBapi ...");
return new Promise(function(resolve, reject){
// do something here
let fbClientScript = document.createElement("script")
// fbClientScript.setAttribute("src", "https://connect.facebook.net/en_US/sdk.js")
fbClientScript.setAttribute("src", "https://connect.facebook.net/pl_PL/all.js")
// Not handle connect timeout case yet
// Append the script onto the head element
// document.head.appendChild(ggClientScript)
// another way
document.getElementsByTagName('head')[0].appendChild(fbClientScript)
// just a log
console.log("FBapi is installed!")
// using promise and you HAVE TO declare resolve() to announce its done! otherwise all others still wait
// but you have to wait for sometime for the js to be applied
setTimeout(function(){
resolve()
}, 500)
});
},
fbInitClient: function(configs){
console.log("Begin FBapi initialization ...");
return new Promise(function(resolve, reject){
// do something. Do init as: https://developers.facebook.com/apps/418176095610807/fb-login/quickstart/
// By default, the fetch_basic_profile parameter of gapi.auth2.init() is set to true, which will automatically add 'email profile openid' as scope.
// Do not use the Google IDs returned by getId() or the user's profile information to communicate the currently signed in user to your backend server.
// Instead, send ID tokens, which can be securely validated on the server.
// console.log(configs) //--> to check if configs
// console.log(window.gapi)
if (configs !== undefined){
window.fbAsyncInit = function(){
FB.init(configs)
}
// Just a log
console.log("FBapi is initialized!")
// using promise and you HAVE TO declare resolve() to announce its done!
setTimeout(function(){
resolve()
}, 500)
} else {
console.log("Undefined configs for FBapi! Initialized failed!")
reject()
}
});
},
fbSignIn: function(successCallback, errorCallback){
// first check current status of user
// More about promise: https://ehkoo.com/bai-viet/tat-tan-tat-ve-promise-va-async-await
// Basics: https://viblo.asia/p/object-trong-javascript-nhung-dieu-can-biet-V3m5W2JWlO7
return new Promise(function(resolve, reject){
// Async here
FB.getLoginStatus(function(response){
if (response.status === "connected"){
// connected - Người đó đăng nhập Facebook và đã đăng nhập ứng dụng của bạn.
console.log("Already signed in and connected.")
console.log(response)
// Ngược lại, dùng `resolve()` để trả dữ liệu về cho `.then()`
resolve(response)
} else {
// then not_authorized or unknown
FB.login(function(response){
if (response.status === "connected"){
// connected - Người đó đăng nhập Facebook và đã đăng nhập ứng dụng của bạn.
console.log("Signed in and connected.")
console.log(response)
resolve(response)
} else {
console.log("Something went wrong while logging in"+response.status)
console.log(response)
reject(response)
}
}, {scope: 'public_profile,email'})
}
})
})
}
}
// You can't mix import and module.exports. In the import world, you need to export things. SO
// See this image: https://i.stack.imgur.com/5WgFJ.png
// https://blog.risingstack.com/node-js-at-scale-module-system-commonjs-require/
// ------------------------------------------------------------------------------
// CommonJS: require + module.exports
// ES6: import + export
// ------------------------------------------------------------------------------
// Change this
module.exports = facebookAuth;
// To this
// export default foo;
// the couple (require/module.exports) by default anyway
// Here: export default foo; ----> In caller: import {fbInstallClient} from "./facebookAuth"; for
// Here: module.exports = facebookAuth; ----> In caller: const fb = require("./facebookAuth"); for
// I hate require so I will use ES6.
// export default facebookAuth;