-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
66 lines (58 loc) · 1.46 KB
/
client.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
import { html, render } from 'https://unpkg.com/lit-html@2.2.0/lit-html.js?module';
const IDP_ORIGIN = 'https://fedcm-idp-demo.glitch.me';
const CLIENT_ID = 'https://fedcm-rp-demo.glitch.me';
export const $ = document.querySelector.bind(document);
export const toast = (text) => {
$('#snackbar').labelText = text;
$('#snackbar').show();
}
export const displayProfile = (profile) => {
render(html`<div>
</div>`, $('#profile'));
}
export const _fetch = async (path, payload = '') => {
const headers = {
'X-Requested-With': 'XMLHttpRequest',
};
if (payload && !(payload instanceof FormData)) {
headers['Content-Type'] = 'application/json';
payload = JSON.stringify(payload);
}
const res = await fetch(path, {
method: 'POST',
credentials: 'same-origin',
headers: headers,
body: payload,
});
if (res.status === 200) {
// Server authentication succeeded
return res.json();
} else {
// Server authentication failed
const result = await res.json();
throw result.error;
}
};
class Loading {
constructor() {
this.progress = $('#progress');
}
start() {
this.progress.indeterminate = true;
}
stop() {
this.progress.indeterminate = false;
}
}
export const loading = new Loading();
export const getCredential = async (hint) => {
return navigator.credentials.get({
federated: {
providers: [{
url: IDP_ORIGIN,
clientId: CLIENT_ID,
hint
}]
}
});
};