forked from kanton-bern/oerebViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esritoken.js
38 lines (32 loc) · 1002 Bytes
/
esritoken.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
import { esriTokenService } from '~/config/setup'
/**
* applies authtentication and fetches a esri token
* @returns {Promise<String>}
*/
export async function fetchEsriToken() {
const settings = {
...esriTokenService,
}
const formData = new URLSearchParams()
formData.append('username', settings.username)
formData.append('password', settings.password)
formData.append('expiration', Math.min(settings.intervalMinutes, 60))
formData.append('f', 'json')
const response = await fetch(settings.endpoint, {
method: 'POST',
body: formData.toString(),
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
},
})
const token = await response.json()
if (!token) {
throw new Error('Expected token as response')
}
if (token.error) {
const message = `Failed fetching token from ${settings.endpoint} with message: ${token.error.message}`
console.error(message, token)
throw new Error(message)
}
return token
}