-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfb.js
142 lines (115 loc) · 3.07 KB
/
fb.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// eslint-disable-next-line no-undef
const fbInitEvent = new Event('FBInit')
window.fbAsyncInit = function () {
// eslint-disable-next-line no-undef
FB.init({
appId: '126508851392364',
cookie: true,
xfbml: true,
version: 'v2.10'
})
// eslint-disable-next-line no-undef
FB.AppEvents.logPageView()
window.dispatchEvent(fbInitEvent)
};
(function (d, s, id) {
var js
var fjs = d.getElementsByTagName(s)[0]
if (d.getElementById(id)) { return }
js = d.createElement(s); js.id = id
js.src = 'https://connect.facebook.net/en_US/sdk.js'
fjs.parentNode.insertBefore(js, fjs)
}(document, 'script', 'facebook-jssdk'))
// Custom utility functions
const awaitFB = new Promise((resolve, reject) => {
try {
// eslint-disable-next-line no-unused-vars
window.addEventListener('FBInit', (event) => {
resolve(true)
})
} catch (e) {
reject(e.message)
}
})
const getAuthStatus = async () => {
await awaitFB
const authStatus = await new Promise((resolve, reject) => {
try {
// eslint-disable-next-line no-undef
FB.getLoginStatus(resolve)
} catch (e) {
reject(e.message)
}
})
return authStatus
}
const checkAuthentication = (authStatus) => {
const isAuthenticated = authStatus && authStatus.status === 'connected'
return isAuthenticated
}
// TODO: do this with RxJX
const awaitFBReAuth = async () => {
await awaitFB
const reAuthStatus = await new Promise((resolve, reject) => {
try {
const callback = authStatus => {
const isAuthenticated = checkAuthentication(authStatus)
if (isAuthenticated) {
// eslint-disable-next-line no-undef
FB.Event.unsubscribe('auth.statusChange', callback)
resolve(authStatus)
}
}
// eslint-disable-next-line no-undef
FB.Event.subscribe('auth.statusChange', callback)
} catch (e) {
reject(e)
}
})
return reAuthStatus
}
const awaitFBAuth = async () => {
await awaitFB
const authStatus = await getAuthStatus()
const isAuthenticated = checkAuthentication(authStatus)
if (isAuthenticated) {
return authStatus
} else {
const reAuthStatus = await awaitFBReAuth()
return reAuthStatus
}
}
export const getAuthKey = async () => {
const authStatus = await awaitFBAuth()
return authStatus.authResponse.accessToken
}
export const fbAPI = async (query) => {
await awaitFBAuth()
const response = await new Promise((resolve, reject) => {
try {
// eslint-disable-next-line no-undef
FB.api(query, resolve)
} catch (e) {
reject(e)
}
})
return response
}
// TODO: do this with RxJX
export const streamAuthStatus = async (onLogin, onLogout) => {
await awaitFB
const _callback = (authStatus) => {
if (authStatus.status) {
const isAuthenticated = checkAuthentication(authStatus)
if (isAuthenticated) {
onLogin()
} else {
onLogout()
}
}
}
// eslint-disable-next-line no-undef
FB.Event.subscribe('auth.statusChange', _callback)
const authStatus = await getAuthStatus()
_callback(authStatus)
}