Commit 5eb1a556 authored by Sylvain Le Bon's avatar Sylvain Le Bon

syntax: refactored

parent b4525d7f
......@@ -3,72 +3,40 @@ if (typeof Oidc == "undefined") {
}
// Var declarations
var sib = {};
sib.oidc = {};
var sib = {oidc: {}};
sib.oidc._applyBindUser = function() {
const id = sib.oidc.user.profile.website
const elements = document.querySelectorAll(`[bind-user]:not([data-src="${id}"])`)
for (const element of elements) {
element.setAttribute('data-src', id)
}
}
// Setup User Manager
sib.oidc._setup = function(settings) {
if (typeof sib.oidc._manager == "undefined") {
sib.oidc._manager = new Oidc.UserManager(settings);
sib.oidc._manager.getUser()
.then(user => {
sib.oidc.user = user;
const id = sib.oidc.user.profile.website
const elements = document.querySelectorAll(`[bind-user]:not([data-src="${id}"])`)
for (const element of elements)
element.setAttribute('data-src', id);
}
}
// Retrieve User from sessionStorage, or fetch it
sib.oidc._getUser = function() {
sib.oidc._manager.getUser()
.then(function(user) {
if (user == null) {
sib.oidc._manager.signinRedirectCallback()
.then(message => {
location.href = message.state
})
.catch(function(error) {
sib.oidc._manager.signinRedirect({
state: location.href,
})
.then()
.catch(function(error) {
console.log(error);
});
});
}
else {
sib.oidc.user = user;
sib.oidc._applyBindUser();
}
})
sib.oidc.handleRedirection = function() {
sib.oidc._manager.signinRedirectCallback()
.then(message => location.href = message.state)
.catch(function(error) {
console.log(error);
sib.oidc._manager.signinRedirect({state: location.href})
.catch(error => {
console.log(error);
});
});
}
// Connect to OIDC provider
sib.oidc._connect = function(settings) {
sib.oidc._setup(settings);
window.addEventListener('DOMContentLoaded', () => sib.oidc._getUser());
if (typeof sib.oidc._manager == "undefined")
sib.oidc._manager = new Oidc.UserManager(settings);
sib.oidc._getUser()
// When user loaded...
sib.oidc._manager.events.addUserLoaded(function(user) {
sib.oidc._getUser();
});
}
// Entry point
const default_settings = {
redirect_uri: document.location.origin
sib.oidc._manager.events.addUserLoaded(user => sib.oidc.handleRedirection());
}
const settings = Object.assign({}, default_settings, document.currentScript.dataset);
const settings = Object.assign({}, {redirect_uri: document.location.origin}, document.currentScript.dataset);
sib.oidc._connect(settings);
\ No newline at end of file
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