-
Notifications
You must be signed in to change notification settings - Fork 0
/
after-auth.js
46 lines (40 loc) · 1.28 KB
/
after-auth.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
import {Cookie, getWwwFormUrlEncodedData} from "./helpers.js";
import {clientId, indexUri, redirectUri} from './config.js'
const queryString = window.location.search
const urlParams = new URLSearchParams(queryString);
const code = urlParams.get('code')
const state = urlParams.get('state')
const tokenEndpoint = Cookie.get("token_endpoint")
const fhirUrl = Cookie.get("fhir_url")
var accessTokenPostBody = {
'grant_type': 'authorization_code',
'code': code,
'client_id': clientId,
'redirect_uri': redirectUri
};
// calls auth server to get access token
async function getAccessToken() {
let response = await fetch(tokenEndpoint, {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: getWwwFormUrlEncodedData(accessTokenPostBody)
})
let token = await response.json()
return token
}
Cookie.rem('token_data')
getAccessToken().then((data) => {
debugger
if (data.error) {
console.log('error fetching token' + data.error_uri)
return
}
Cookie.set('token_data', JSON.stringify(data), {secure: true, "max-age": 900})
location.assign(indexUri)
}).catch((err) => {
debugger
console.log('error fetching access token')
})