Commit 0f877666 authored by Clément's avatar Clément

update: simplify sib-oidc

parent 61783a90
......@@ -5,20 +5,32 @@
<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
<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"
data-response_type="id_token token"
data-scope="openid profile email"
data-automaticSilentRenew="true"
data-loadUserInfo="true"
></script>
</head>
<body>
<h1>sib-oidc</h1>
<button id="reset">clear storage and set new URL</button>
<button id="reload">reload</button>
<script>
reset.onclick = () => {
window.sessionStorage.clear();
window.localStorage.clear();
history.pushState({}, '', '.?time=' + new Date().toLocaleTimeString());
};
reload.onclick = () => location.reload();
</script>
<pre id="output"></pre>
<script>
sib.oidc.getUser(user => {
output.textContent = JSON.stringify(user, null, 2);
});
</script>
</body>
</html>
\ No newline at end of file
</html>
if (typeof Oidc == "undefined") {
throw new Error("You are missing the `oidc-client-js` lib");
}
{
const sib = { oidc: {} };
window.sib = sib;
// Var declarations
var sib = {oidc: {}};
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',
};
sib.oidc.getUser = function() {
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);
}
}
const settings = Object.assign(
{},
defaultSettings,
document.currentScript.dataset,
Please register or sign in to reply
);
// Retrieve User from sessionStorage, or fetch it
sib.oidc.handleRedirection = function() {
sib.oidc._manager.signinRedirectCallback()
.then(message => location.href = message.state)
.catch(function(error) {
sib.oidc._manager.signinRedirect({state: location.href})
.catch(error => {
console.log(error);
});
});
}
const manager = new Oidc.UserManager(settings);
// Connect to OIDC provider
sib.oidc.connect = function(settings) {
if (typeof sib.oidc._manager == "undefined")
sib.oidc._manager = new Oidc.UserManager(settings);
sib.oidc.getUser()
const promise = tryToGetUserOrConnect();
sib.oidc.getUser = cb => {
promise.then(cb);
};
// When user loaded...
sib.oidc._manager.events.addUserLoaded(user => sib.oidc.handleRedirection());
}
sib.oidc.getUser(user => {
sib.oidc.user = user;
});
const settings = Object.assign({}, {redirect_uri: document.location.origin}, document.currentScript.dataset);
sib.oidc.connect(settings);
\ No newline at end of file
async function tryToGetUserOrConnect() {
const user = await manager.getUser();
if (user) {
return user;
}
try {
const user = await manager.signinRedirectCallback();
location.href = user.state;
return user;
} catch (e) {
try {
manager.signinRedirect({
state: location.href,
});
} catch (e) {
console.log(e);
}
}
}
}
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