Commit 9765d1a3 authored by Nicolas Mérigot's avatar Nicolas Mérigot

refactor: avoid uselesss install function

parent 9aca2675
Pipeline #1973 passed with stage
in 27 seconds
......@@ -13,47 +13,62 @@ const defaultSettings = {
class SIBOidc extends HTMLElement {
constructor() {
super();
this.getUser = install(this.dataset);
this.manager = null;
this.user = null;
// duplicate default settings in this.settings
this.settings = Object.assign({}, defaultSettings);
}
}
customElements.define('sib-oidc', SIBOidc);
connectedCallback() {
this.settings = Object.assign({}, defaultSettings, this.dataset);
function install(settings) {
settings = Object.assign({}, defaultSettings, settings);
// initialize OIDC Manager
this.manager = new UserManager(this.settings);
const manager = new UserManager(settings);
// add a hook on DOMContentLoaded
document.addEventListener('DOMContentLoaded', async () => {
const user = await this.getUser();
const id = user.profile.website;
const selector = `[bind-user]:not([data-src="${id}"])`;
const elements = document.querySelectorAll(selector);
for (const element of elements) {
element.setAttribute('data-src', id);
}
});
}
const promise = tryToGetUserOrConnect();
disconnectedCallback() {
this.manager = null;
this.user = null;
}
const getUser = () => promise;
document.addEventListener('DOMContentLoaded', async () => {
const user = await getUser();
const id = user.profile.website;
const selector = `[bind-user]:not([data-src="${id}"])`;
const elements = document.querySelectorAll(selector);
for (const element of elements) {
element.setAttribute('data-src', id);
async getUser() {
// if user is already set, return it
if (this.user) {
return this.user;
}
});
async function tryToGetUserOrConnect() {
const user = await manager.getUser();
// try to get user through OIDC Manager
const user = await this.manager.getUser();
if (user) {
this.user = user;
return user;
}
try {
const user = await manager.signinRedirectCallback();
const user = await this.manager.signinRedirectCallback();
location.href = user.state;
return user;
} catch (e) {
manager.signinRedirect({
this.manager.signinRedirect({
state: location.href,
});
throw `Can't get user, redirect to OIDC authority ${settings.authority}`;
throw `Can't get user, redirect to OIDC authority ${this.settings.authority}`;
}
}
return getUser;
}
export { install, SIBOidc };
customElements.define('sib-oidc', SIBOidc);
export { 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