-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
53 lines (50 loc) · 1.67 KB
/
index.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
import { Linking } from 'react-native'
const authorizationUrl = (url, appId, callback, scope, responseType = 'token') =>
`${url}?scope=${encodeURIComponent(scope)}&
redirect_uri=${encodeURIComponent(callback)}&
response_type=${responseType}&
client_id=${appId}`.replace(/\s+/g, '')
export default class {
constructor(clientId, callback, authUrl, tokenUrl) {
this.authenticate = this.authenticate.bind(this)
this.clientId = clientId
this.callback = callback
this.authUrl = authUrl
this.tokenUrl = tokenUrl
}
authenticate() {
return new Promise((resolve, reject) => {
const handleUrl = event => {
const authCode = event.url.substring(
event.url.indexOf('=') + 1,
event.url.length
)
const tokenRequest = {
code: authCode,
client_id: this.clientId,
redirect_uri: this.callback,
grant_type: 'authorization_code'
}
let s = []
for (let key in tokenRequest) {
if (tokenRequest.hasOwnProperty(key)) {
s.push(`${encodeURIComponent(key)}=${encodeURIComponent(tokenRequest[key])}`)
}
}
fetch(this.tokenUrl, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: s.join('&')
})
.then(response => resolve(response))
.catch(error => reject(error))
Linking.removeEventListener('url', handleUrl)
}
Linking.addEventListener('url', handleUrl)
Linking.openURL(authorizationUrl(this.authUrl, this.clientId, this.callback, 'code'))
})
}
}