Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update sso demo logout #23

Merged
merged 1 commit into from
Apr 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 118 additions & 94 deletions src/examples/SingleSignOn/SingleSignOn.js
Original file line number Diff line number Diff line change
@@ -1,114 +1,138 @@
import * as FormioLoader from "../../matrixHelpers/FormioLoader";

export const createSSoForm = () => {
const formioApiDomain = "api.forms.platforms.qld.gov.au";
const formioProjectId = "ncwawujlwylhrfy"; // configure in squiz component
const formioLoginFormId = "oidcsso"; // configure in squiz component
const formioServiceFormId = "devauthform"; // configure in squiz component
const namespace = `formio_${formioProjectId}`;
const div = document.createElement("div");
div.innerHTML = `
<div id="oidc_form"></div>
<div id="formio"></div>
`;
let oidcform;
let formioDiv;

const oidcform = div.querySelector("#oidc_form");
const formioDiv = div.querySelector("#formio");
const resetDiv = () => {
div.innerHTML = `
<div id="oidc_form"></div>
<div id="formio"></div>
`;

function appendSpinner(parentElement) {
oidcform = div.querySelector("#oidc_form");
formioDiv = div.querySelector("#formio");
};

const appendSpinner = (parentElement) => {
parentElement.innerHTML = `<img src="https://www.qld.gov.au/__data/assets/image/0019/126703/Spinner-1s-200px.png"/>`;
}
};

setTimeout(() => {
const formioApiDomain = "api.forms.platforms.qld.gov.au";
const formioProjectId = "ncwawujlwylhrfy"; // configure in squiz component
const formioLoginFormId = "oidcsso"; // configure in squiz component
const formioServiceFormId = "devauthform"; // configure in squiz component
const namespace = `formio_${formioProjectId}`;
// const formioBaseEndpoint = `https://api.forms.platforms.qld.gov.au/${formioProjectId}`;
// const formioLoginFormEndpoint = `${formioBaseEndpoint}/${formioLoginFormId}?live=1`;
// const formioServiceFormEndpoint = `${formioBaseEndpoint}/${formioServiceFormId}`;
let user = null;
// let oidcForm = null;
// const realForm = null;

function realFormSetup() {
console.info("Found user, redirecting to the form...");
// realForm = Formio.createForm(
// document.getElementById("formio"),
// formioServiceFormEndpoint
const pickForm = () => {
const user = Formio.getUser({ namespace });
console.info("user", user);
if (user) {
appendSpinner(formioDiv);
// eslint-disable-next-line no-use-before-define
realFormSetup();
} else {
appendSpinner(oidcform);
// eslint-disable-next-line no-use-before-define
loginFormSetup();
}
};

const logout = (form) => {
// currently the logout endpoint has cors issue, otherwise could using the approach below without reload the page:
// Formio.makeStaticRequest(
// "https://uat.auth.qld.gov.au/auth/realms/tell-us-once/protocol/openid-connect/logout",
// "GET",
// null,
// { namespace }
// ).then(() => {
// Formio.logout(form.formio, {
// namespace,
// }).then(() => {
// resetDiv();
// pickForm();
// });
// });

Formio.logout(form.formio, {
namespace,
}).then(() => {
// window.location.reload();
const popup = window.open(
"https://uat.auth.qld.gov.au/auth/realms/tell-us-once/protocol/openid-connect/logout",
"_logout",
"location=no,height=100,width=100,scrollbars=no,status=no"
);
// can't use addEventListener to check pop is loaded if they are different domains due to CORS
// popup.addEventListener(
// "load",
// () => {
// popup.close();
// },
// false
// );
// settle to use timeout for now
setTimeout(() => {
popup.close();
resetDiv();
pickForm();
}, 1000);
});
};

const createFormController = ({ form }) => {
// window.form = form;
// This section of code is the "Form Controller"
form.on("submitDone", function submitDoneCleanup(submission) {
console.info("submission", submission);

Formio.logout(form.formio, {
namespace,
}).then(() => {
window.location.reload();
});
});
};

FormioLoader.initFormioInstance(formioDiv, {
projectName: formioProjectId,
formName: formioServiceFormId,
envUrl: formioApiDomain,
namespace,
createFormController,
const realFormSetup = () => {
console.info("Found user, redirecting to the form...");

const createFormController = ({ form }) => {
// This section of code is the "Form Controller"
form.on("submitDone", function submitDoneCleanup(submission) {
console.info("submission", submission);
logout(form);
});
}

function loginFormSetup() {
console.info("No current user, rendering oidc sso form...");
// oidcForm = Formio.createForm(
// document.getElementById("oidc_form"),
// formioLoginFormEndpoint
// );
form.on("logout", () => {
logout(form);
});
};
FormioLoader.initFormioInstance(formioDiv, {
projectName: formioProjectId,
formName: formioServiceFormId,
envUrl: formioApiDomain,
namespace,
createFormController,
});
};

const loginFormSetup = () => {
console.info("No current user, rendering oidc sso form...");

const createFormController = ({ form }) => {
console.info(`Loaded form: ${form.formio.formUrl}`, form);
// console.info(JSON.stringify(form.formio));
form.on("submitDone", (submission) => {
console.info("submission", submission);

const createFormController = ({ form }) => {
console.info(`Loaded form: ${form.formio.formUrl}`, form);
// console.info(JSON.stringify(form.formio));
form.on("submitDone", (submission) => {
console.info("submission", submission);

form.formio.currentUser({ namespace }).then((userDetails) => {
// clean up URL paramters from submission or logout redirect
console.info("userDetails", userDetails);
// const parent = document.getElementById("oidc_form").parentElement;
oidcform.remove();
// const oidcformNew = document.createElement("div");
// oidcformNew.setAttribute("id", "oidc_form");
// parent.appendChild(oidcform);
// eslint-disable-next-line no-use-before-define
pickForm();
// const formioCleanURL = window.location.href.split("?")[0];
//
// // trigger post-login form to load
// window.location.href = formioCleanURL;
});
form.formio.currentUser({ namespace }).then((userDetails) => {
// clean up URL paramters from submission or logout redirect
console.info("userDetails", userDetails);
resetDiv();
pickForm();
});
};

FormioLoader.initFormioInstance(oidcform, {
projectName: formioProjectId,
formName: formioLoginFormId,
envUrl: formioApiDomain,
namespace,
createFormController,
});
}
};

function pickForm() {
user = Formio.getUser({ namespace });
console.info("user", user);
if (user) {
appendSpinner(formioDiv);
realFormSetup();
} else {
appendSpinner(oidcform);
loginFormSetup();
}
}
FormioLoader.initFormioInstance(oidcform, {
projectName: formioProjectId,
formName: formioLoginFormId,
envUrl: formioApiDomain,
namespace,
createFormController,
});
};

resetDiv();

setTimeout(() => {
pickForm();
}, 100);

Expand Down