Commit 9aca2675 authored by Clément's avatar Clément

feature: settings attributs are now passed to <sib-oidc> element

parent 41181613
Pipeline #1972 passed with stage
in 27 seconds
......@@ -5,15 +5,13 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>SIB-OIDC test</title>
<script src="./node_modules/oidc-client/dist/oidc-client.js"></script>
<script
src="sib-oidc.js"
data-authority="https://test-paris.happy-dev.fr/openid/"
data-client_id="833925"
></script>
<script type="module" src="sib-oidc.js"></script>
</head>
<body>
<sib-oidc
data-authority="https://test-paris.happy-dev.fr/openid/"
data-client_id="833925"
></sib-oidc>
<h1>sib-oidc</h1>
<button id="reset">clear storage and set new URL</button>
<button id="reload">reload</button>
......@@ -28,8 +26,13 @@
<pre id="output"></pre>
<script>
sib.oidc.getUser().then(user => {
output.textContent = JSON.stringify(user, null, 2);
sibOidc = document.querySelector('sib-oidc');
customElements.whenDefined('sib-oidc').then(() => {
sibOidc.getUser().then(user => {
console.log(user);
output.textContent = JSON.stringify(user, null, 2);
});
});
</script>
</body>
......
import 'https://unpkg.com/oidc-client@1.6';
export default function install () {
const defaultSettings = {
redirect_uri: location.origin,
// authority: 'https://test-paris.happy-dev.fr/openid/',
// client_id: '833925',
response_type: 'id_token token',
scope: 'openid profile email',
automaticsilentrenew: 'true',
loaduserinfo: 'true',
};
const settings = Object.assign(
{},
defaultSettings,
document.currentScript.dataset,
);
const defaultSettings = {
redirect_uri: location.origin,
// authority: 'https://test-paris.happy-dev.fr/openid/',
// client_id: '833925',
response_type: 'id_token token',
scope: 'openid profile email',
automaticsilentrenew: 'true',
loaduserinfo: 'true',
};
class SIBOidc extends HTMLElement {
constructor() {
super();
this.getUser = install(this.dataset);
}
}
const manager = new Oidc.UserManager(settings);
customElements.define('sib-oidc', SIBOidc);
const promise = tryToGetUserOrConnect();
function install(settings) {
settings = Object.assign({}, defaultSettings, settings);
const sib = {
oidc: {
getUser: () => promise,
user: null,
},
};
window.sib = sib;
const manager = new UserManager(settings);
sib.oidc.getUser().then(user => {
sib.oidc.user = user;
});
const promise = tryToGetUserOrConnect();
const getUser = () => promise;
document.addEventListener('DOMContentLoaded', async () => {
const user = await sib.oidc.getUser();
const user = await getUser();
const id = user.profile.website;
const selector = `[bind-user]:not([data-src="${id}"])`;
const elements = document.querySelectorAll(selector);
......@@ -59,6 +53,7 @@ export default function install () {
throw `Can't get user, redirect to OIDC authority ${settings.authority}`;
}
}
return getUser;
}
install();
export { install, SIBOidc };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment