viewof user = {
const url = html`<a href>`.href;
if (
!url.startsWith("https://observablehq.com") &&
!url.startsWith("https://next.observablehq.com")
) {
throw new Error("Login form is not allowed to be embedded");
}
const authContainerId = `firebaseui-auth-container-${Math.random()
.toString(36)
.substring(7)}`;
const form = html`
<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/4.6.1/firebase-ui-auth.css" />
<div id="${authContainerId}"></div>
`;
let delayInitialValue = new Promise(async (resolveInitialValue) => {
firebaseConfig.uiConfig = firebaseConfig.uiConfig || {};
firebaseConfig.uiConfig.signInFlow = "popup";
firebaseConfig.uiConfig.callbacks = {
signInSuccessWithAuthResult: (authResult, redirectUrl) => false
};
let firstTime = true;
firebase.auth().onAuthStateChanged(function (user) {
if (user != null || !firstTime) {
firstTime = false;
form.value = user;
resolveInitialValue(user);
form.dispatchEvent(new CustomEvent("input"));
}
});
function pollForUIContainer() {
window.firebase = window._firebase;
if (!document.getElementById(authContainerId)) {
setTimeout(pollForUIContainer, 50);
} else {
console.log(`starting for ${authContainerId}`);
const ui =
firebaseui.auth.AuthUI.getInstance() ||
new firebaseui.auth.AuthUI(firebase.auth());
ui.start(`#${authContainerId}`, firebaseConfig.uiConfig);
}
}
pollForUIContainer();
});
form.value = delayInitialValue;
return form;
}